Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Hotkey only if game is active?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
jgoemat
Master Cheater
Reputation: 23

Joined: 25 Sep 2011
Posts: 264

PostPosted: Sun Jan 05, 2020 7:10 pm    Post subject: Hotkey only if game is active? Reply with quote

It would be nice if I could make hotkeys or scripts only active if the game window is active, is there a built-in way to do this?

I was thinking of using LUA to call GetActiveWindow and get the window text, but that seems like a pain and prone to problems... Creating a main script for the cheat and having lua ran by the hotkeys to enable/disable the script seems like a lot of bother too...

Mainly I'm thinking for scripts that would only be activated via hotkey in-game, so something like this would be nice:

Code:
assertActiveWindowTitle("Red Dead Redemption 2")
Back to top
View user's profile Send private message
AylinCE
Grandmaster Cheater Supreme
Reputation: 37

Joined: 16 Feb 2017
Posts: 1533

PostPosted: Sun Jan 05, 2020 10:01 pm    Post subject: Reply with quote

The source code is published here.
https://forum.cheatengine.org/viewtopic.php?t=611429


Code:
--[[
Process Monitor [procmon.lua]
(c) 2019 atom0s

Simple script to monitor the running process list for certain executable names. If a match is found,
CE will terminate.

Edit the 'procMonNames' to add new processes to monitor.
Edit the 'Interval' to adjust how often this scans. (Currently once per second.)
]]--

-- The list of processes to look for and terminate if found..
local procMonNames = { 'notepad.exe', 'regedit.exe' }; -- or Game.exe

-- Determines if the given process name exists in the procMonNames table..
local function hasName(n)
    n = string.lower(n);
    for k, v in ipairs(procMonNames) do
        if (n == v) then
            return true;
        end
    end
    return false;
end

-- Start a background timer to monitor the process list..
local procMonTimer = procMonTimer or createTimer();
procMonTimer.Interval = 1000;
procMonTimer.OnTimer = function(t)
    -- Obtain the current process list..
    local procs = getProcesslist();

    -- Check for bad processes to kill CE..
    for k, v in pairs(procs) do
        if (hasName(v)) then
            closeCE(); -- or active HotKey ..
        end
    end
end
procMonTimer.setEnabled(true);

_________________
Hi Hitler Different Trainer forms for you!
https://forum.cheatengine.org/viewtopic.php?t=619279
Enthusiastic people: Always one step ahead
Do not underestimate me Master: You were a beginner in the past
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1668

PostPosted: Sun Jan 05, 2020 10:55 pm    Post subject: Reply with quote

The process monitor such as on:
https://forum.cheatengine.org/viewtopic.php?t=611429

made by atom0s, is to control and terminate CE automatically when some processes found.

If I not misunderstood, what jgoemat need is a function to handle a focused window. So, he wants to control it by Title Name or Window Name.
There are scripts like this somewhere in this forum:

Code:
function EnumWindows()
  if not readIntegerLocal"_enumWin" or readIntegerLocal"_enumWin+f8"~=1 then
    local script= string.format([[
globalalloc(_enumWin,$4000)
define(cave,_enumWin)
define(proc,_enumWin+80)
define(wcnt,_enumWin+f0)
define(save,_enumWin+100)

cave+f8:
dd 1

cave:
push  rsi
push  rdi
  xor rax,rax
  mov rdi,wcnt
  mov [rdi],rax
[32-bit]
  push  0
  push  proc
  call  EnumWindows
[/32-bit]
[64-bit]
  xor   rdx,rdx
  mov   rcx,proc
  mov   rax,EnumWindows
  sub   rsp,20
  call  rax
  add   rsp,20
[/64-bit]
pop   rdi
pop   rsi
[32-bit]
ret   4
[/32-bit]
[64-bit]
ret
[/64-bit]


proc:
push  rcx
[32-bit]
mov   rcx,[rsp+08]
[/32-bit]
push  rdx
push  rdi
push  rsi
  mov rdx,wcnt
  mov rsi,[rdx]
  mov rdi,save
  mov [rdi+rsi*8],rcx
  inc dword ptr[rdx]
  cmp dword ptr[rdx],7c0 //  ~(4000-200)/8
  jle %s @f
  xor rax,rax
@@:
pop   rsi
pop   rdi
pop   rdx
pop   rcx
[32-bit]
ret   8
[/32-bit]
[64-bit]
ret
[/64-bit]
    ]],cheatEngineIs64Bit() and 'short' or '')
    if not cheatEngineIs64Bit() then
      script = script:gsub("%f[%w]r([abcds])([xip])","e%1%2")
    end
    autoAssemble(script,true)
--    print(tostring(autoAssemble(script,true)),script)
  end -- autoAssemble

  executeCodeLocal"_enumWin"

  local addr = GetAddress("_enumWin+100",true)
  local wcnt = readIntegerLocal"_enumWin+f0"
  local ret = {}
  for i=0,wcnt-1 do
    local hwnd = readIntegerLocal(addr+i*8)
    if not hwnd or hwnd==0 then break else
      ret[1+#ret]=hwnd
    end
  end
  return ret
end

function GetWindowByCaptionPID(cap,pid,onlyVisible)
  onlyVisible = onlyVisible ~= false
  local r,w = {},EnumWindows()
  cap,pid = tostring(cap),pid or GetOpenedProcessID()
  for i=1,#w do
    local hwnd = w[i]
    if pid == getWindowProcessID(hwnd) then
      local max,wn,own = 1000,hwnd
      while own==nil or own~=0 and own~=wn and max>0 do
        wn,own,max = own or wn,GetWindow(wn,4),max-1
        local txt = getWindowCaption(wn)
        if txt:lower():find(cap:lower(),1,true) then
          if not onlyVisible or 0~=executeCodeLocal("IsWindowVisible",wn) then
            return wn
          end
        end
      end
    end
  end
  return 0
end

function SetWindowCaption(hwnd,caption)
  if hwnd~=0 then
     local lpStr = createMemoryStream()
     lpStr.write(stringToByteTable(tostring(caption).."\0"))
     sendMessage(hwnd,0xc--[[WM_SETTEXT ]],nil--[[not used]],lpStr.Memory)
     lpStr.Destroy()
    end
end

function TitleText(HWNDOrOriginalCaption,pid,onlyVisible)
  local wn
  if type(HWNDOrOriginalCaption)=='number' then
    wn = HWNDOrOriginalCaption
  elseif type(HWNDOrOriginalCaption)=='string' then
    wn = GetWindowByCaptionPID(HWNDOrOriginalCaption,pid,onlyVisible)
  else
    local pid = pid or GetOpenedProcessID()
    if pid and pid~=0 then
      wn = GetWindowByCaptionPID(getWindowlist()[pid],pid,onlyVisible)
    end
  end
  if wn and wn~=0 then
    return function(newCaption)
      SetWindowCaption(wn,newCaption)
    end
  end
end
 

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
panraven
Grandmaster Cheater
Reputation: 62

Joined: 01 Oct 2008
Posts: 958

PostPosted: Mon Jan 06, 2020 6:56 am    Post subject: Reply with quote

+1 your suggestion, mixing hotkey effect on ce and attached process is troublesome, and extending CE hotkey handling should work for hotkey definition with Memory record that save the work for writing custom lua script.

Otherwise, I would go the 'bother' way, try detect if getOpenedProcessID()==getForegroundProcess() and only run OnHotkey function if so.

Code:
function RunIfAttachedIsForeGround(fn,...)
  if getOpenedProcessID()==getForegroundProcess() then
    return fn(...)
  end
end

function createHotkeyOnAttachedIsForeGround(fn, ...)
  return createHotkey(function(hko)
    return RunIfAttachedIsForeGround(fn, hko)
  end, ...)
end

-- test
if HK then HK = nil,HK.Destroy()end
local cnt = 0
HK = createHotkeyOnAttachedIsForeGround(function(hko)
  cnt = cnt + 1
  local cap = MainForm.Caption
  MainForm.Caption =  (cap:match"%s*([^:]+)%s*:" or cap)..":"
    ..string.format("%X, %3d",getForegroundProcess(), cnt % 1000)
end, VK_LCONTROL, VK_RSHIFT)

_________________
- Retarded.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites