Page 1 of 1

MM doesn't react on Start-Key

Posted: Wed Aug 14, 2013 2:16 pm
by aliex
Hello! I got a problem using the latest MM version 1.03. MM doesn't recognize when i press the Start or Stop Button unless i activate the window. This is new, 1.02 did so. I am not able to deactivate the running macro when i use the screen the game is running. I got 2 Monitors, the problem occured also when i use the MM window on the same screen as the game.
Help, anyone?

Re: MM doesn't react on Start-Key

Posted: Wed Aug 14, 2013 2:42 pm
by Administrator
Let me see if I understand you correctly. You are saying that when you are using MicroMacro in attached mode, it doesn't detect start/stop hotkeys when the target window is focused? That is, for the RoM bot, if you have RoM on top and press F5, it doesn't start?

When you upgraded, did you copy all files and not just micromacro.exe?

Re: MM doesn't react on Start-Key

Posted: Wed Aug 14, 2013 11:57 pm
by aliex
Administrator wrote:Let me see if I understand you correctly. You are saying that when you are using MicroMacro in attached mode, it doesn't detect start/stop hotkeys when the target window is focused? That is, for the RoM bot, if you have RoM on top and press F5, it doesn't start?

When you upgraded, did you copy all files and not just micromacro.exe?
Its not the RomBot and i didn't attach the target window. I tried to use the standard F5 key also, doesn't work either.
The Problem is occuring even before the macro is running. I am forced to klick in the command window of MM to start it. When the window is not activated MM doesn't react to F5 or any other defined Start-Key.

And yes, i did a clean installement, including the complete micromacro dir. I just included my scripts dir in the new installement.

[Edit] I tried to run the Game in Window and Fullscreen Mode. No Difference. I am using a Bot which controls also the mouse and now, when the bot is running, i cant stop the macro at all since i cant move the mouse to the command window of MM, focus it, and press the pause/stopp button. this is really really bad, i really need help in that case.

Re: MM doesn't react on Start-Key

Posted: Thu Aug 15, 2013 6:08 pm
by Administrator
I think this was disabled when updating to Lua 5.2. I don't remember the exact reasons for it, though.
You can edit your lib/lib.lua, line 492 to change this:

Code: Select all

	if fw ~= getHwnd() and ( ah == 0 or fw ~= ah ) then
Removing "ah == 0 or" from that line should do the trick.

Re: MM doesn't react on Start-Key

Posted: Fri Aug 16, 2013 4:42 am
by rock5
Administrator wrote:Removing "ah == 0 or" from that line should do the trick
But doesn't that mean that no matter what program is the active window, if you press the 'stop' key it will stop the bot? So if you are using some other program or application that uses the same key combination, eg. F6, you can unknowingly cause the bot to stop. I think that was the reason we added that. You could also accidentally stop other running bots that are running. The problem is if MM isn't attached then it can't know which program it is associated with.

I wonder if we could check an associated window anyway. Example, with rombot, even if we were to detach from the game for whatever reason or not attach at all like with getid and getpos, we would still know which window we are associated with because of the __WIN and __PROC variables. I'm not sure how we could make it work, maybe an MM global variable or a function, but it should be possible. Then we coul have the keys check check the associated window as well.

Re: MM doesn't react on Start-Key

Posted: Fri Aug 16, 2013 4:28 pm
by Administrator
I agree. I don't want to make that specific change due to those exact reasons. I guess we could add a function (targetWindow() or something similar?) that will set the HWND of a specific window to also check against hotkeys but without attaching to it. Basically, attach() without the keyboard/mouse hooks.

Re: MM doesn't react on Start-Key

Posted: Sat Aug 17, 2013 1:39 am
by rock5
So after we set the target you would do something like?

Code: Select all

	local check = true;
	local ah = getAttachedHwnd();
	local fw = foregroundWindow();
	local tw = getTargetWindow()
	if fw ~= getHwnd() and ( ah == 0 or fw ~= ah ) and (tw == 0 or fw ~= tw) then
		check = false;
	end

Re: MM doesn't react on Start-Key

Posted: Sat Aug 17, 2013 2:31 pm
by Administrator
Something like that, yeah. It could probably all just be done in lib.lua, using a local variable, so we wouldn't even need the getTargetWindow() function.

Re: MM doesn't react on Start-Key

Posted: Sat Aug 17, 2013 4:13 pm
by rock5
I get it. Use the targetwindow function to set a variable and then just use the variable.

Re: MM doesn't react on Start-Key

Posted: Mon Aug 19, 2013 2:37 pm
by aliex
Administrator wrote:I think this was disabled when updating to Lua 5.2. I don't remember the exact reasons for it, though.
You can edit your lib/lib.lua, line 492 to change this:

Code: Select all

	if fw ~= getHwnd() and ( ah == 0 or fw ~= ah ) then
Removing "ah == 0 or" from that line should do the trick.
Line 492 reads now:

Code: Select all

if fw ~= getHwnd() and ( fw ~= ah ) then
as mentioned above but i still need to focus the MM command-window to stop the script. :?

Re: MM doesn't react on Start-Key

Posted: Wed Aug 21, 2013 2:05 pm
by Administrator
Well I found the problem and it is because I overlooked one simple thing: keyboardState() will only grab data from the application's keyboard state and any attached inputs; it will not poll the hardware directly.

I guess there's two things that could be done: go back to using keyPressed() for each key, or look into using some other function for polling the keyboard. Microsoft's Win32 implementation of GetKeyboardState() is buggy, anyways, so I'm not against abandoning it, however I don't want to rely on any big libraries, either.


Here's a (somewhat heavy) modification to lib.lua:

Code: Select all

-- Set a target window that we check for hotkeys from
-- but do not attach to it.
local __targetWindow = nil;
function targetWindow(hwnd)
	__targetWindow = hwnd;
end

-- Get debug information from any running script
--[[
local script_line = 0;
local script_name = "";
local function get_script_info(event, line)
	script_line = line;
	script_name = debug.getinfo(2).short_src;
end
debug.sethook(get_script_info, "l");
]]

local ks = keyboardState();
local lastKS = ks;
local function checkGlobalHotkeys()
	local script_status = 'ok';

	local function pressed(vk)
		if( ks[vk] and not lastKS[vk] ) then
			return true;
		else
			return false;
		end
	end

	lastKS = table.copy(ks);

	local check = true;
	local ah = getAttachedHwnd();
	local fw = foregroundWindow();
	if fw ~= getHwnd() and ( ah == 0 or fw ~= ah ) and (__targetWindow and __targetWindow ~= fw) then
		check = false;
	end

	if( check ) then
		ks[startKey] = keyPressed(startKey);
		ks[stopKey] = keyPressed(stopKey);
		ks[key.VK_L] = keyPressed(key.VK_L);
		ks[key.VK_CONTROL] = keyPressed(key.VK_CONTROL);

		if( not __PErunning and pressed(startKey)) then
			script_status = 'start';
		elseif( __PErunning and pressed(stopKey) ) then
			script_status = 'stop';
		elseif( ks[key.VK_CONTROL] and pressed(key.VK_L) ) then
			script_status = 'kill';
		end
	else
		ks[startKey] = false;
		ks[stopKey] = false;
		ks[key.VK_L] = false;
		ks[key.VK_CONTROL] = false;
	end

	return script_status;
end
function targetWindow() added and function checkGlobalHotkeys() replaced.

Line ~641:

Code: Select all

	while( true ) do
		--lastKS = ks;
		--ks = keyboardState();
		local script_status = checkGlobalHotkeys();
		--[[if( script_status ~= 'ok' ) then
			print("Status", script_status);
		end]]
Comment out the two lines below the while( true ) by adding '--' in front of them.



Now after these modifications, call targetWindow() outside of main() in your script, like so:

Code: Select all

targetWindow(findWindow("The game"));
function main()
...
end