View unanswered posts | View active topics It is currently May 21st, '13, 07:34




Reply to topic  [ 5 posts ] 
 input.VK_LCONTROL and input.VK_RCONTROL 
Author Message

Joined: Jan 17th, '12, 06:03
Posts: 3
Post input.VK_LCONTROL and input.VK_RCONTROL
G'day. I'm working on a little macro script but the left and right control keys don't appear to be registering or staying pressed (I can't tell which). The code in question is simply:

Code:
input.KeyHold(input.VK_LCONTROL, game_window)
input.KeyPress(input.VK_F5, game_window)
input.KeyRelease(input.VK_LCONTROL, game_window)


Cheers.

PS: I don't understand why MacroMonkey hasn't got heaps of attention. Lua is much nicer than the alternatives in AutoHotkey and Autoit.


Jan 19th, '12, 02:17
Profile
Site Moderator
User avatar

Joined: Aug 18th, '09, 03:32
Posts: 1201
Post Re: input.VK_LCONTROL and input.VK_RCONTROL
Does the F5 key work? Or for that matter do any keys work at all this way?

Assuming your window handle is valid this will be doing the windows message way.
Internally using WM_KEYDOWN and up type messages. Which is normally preferable if it will work for the target.

If you don't supply the windows handle then MM will use the global SendInput() way.
Which is not so ideal because it's global and your application will probably have to be in focus for it to work.

There is more to it in MM of course. Special key translations, managing the shift key states, etc.

Key inputs can be kind of tricky, in particular if your target is a game.
It can be using DirectInput for the keyboard in which case the message way probably won't work.
Also occasionally some applications will use raw inputs.

For reference: http://www.macromonkey.com/userinput.html
There could be a bug in MM, in particular with CTRL key handling, but the whole fake input system has been tested pretty well.
You probably just need to troubleshoot and see first if it will process inputs the message way first.

Unless you want to keep it private, what is the target application?


Jan 20th, '12, 08:46
Profile

Joined: Jan 17th, '12, 06:03
Posts: 3
Post Re: input.VK_LCONTROL and input.VK_RCONTROL
G'day Sirmabus. Yep the F5 key and all other keys I've tried work. I'm using VK_SHIFT with a key hold and it works fine also, just not any of the control keys it seems because I tried VK_CONTROL and VK_RCONTROL as well with no luck. In the end to work around the issue I rebound the keybinding in question from control + F5 to just F6 on it's own. The game in question is Starcraft II, I'm cheating on my injects when I play Zerg.

This is the original script with the VK_LCONTROL key hold in place that doesn't work. The script is still evolving and I've never written Lua before I wrote this so if you have any suggestions or ideas:

Code:
-- Inject alarm.
-- TODO: Module: time.Alarm(31, func) ?
local alarm_time = nil

function alarm_tick()
  if alarm_time then
    if time.Delta(alarm_time) >= 31 then
      alarm_time = nil
      audio.PlaySound('inject.wav')
    end
  end
end

function alarm_start()
  if not alarm_time then
    alarm_time = time.Get()
  end
end

-- Inject.
function inject(sc2)
  -- Save viewport position.
  input.KeyHold(input.VK_LCONTROL, sc2)
  input.KeyPress(input.VK_F5, sc2)
  input.KeyRelease(input.VK_LCONTROL, sc2)

  -- Backspace method.
  input.KeyPress("5", sc2) -- Queens.
  input.KeyPress("x", sc2) -- Grid Inject.
  input.KeyPress(input.VK_BACK, sc2)
  input.KeyHold(input.VK_SHIFT, sc2)
  for i=1,6 do
    input.KeyPress(input.VK_BACK, sc2)
    input.MouseClick(input.MOUSE_LEFT, sc2, 955, 400)
    time.Sleep(0.005)
  end
  input.KeyRelease(input.VK_SHIFT, sc2)

  -- Recall viewport.
  input.KeyPress(input.VK_F5, sc2)
  -- Switch to all hatches key.
  input.KeyPress("4", sc2)
end

-- TODO: Move from assignment to whatever inject will be.
-- Hopefully '55' if I can be arsed figuring out a reliable double click timer.
function queen_inject()
  return input.KeyGetState("i") > 0
end

-- Main.
local sc2 = win.Find("StarCraft II", nil, 0,0, win.FIND_TILE_INSIDE)
while true do
  if sc2 then
    if queen_inject() then
      inject(sc2)
      alarm_start()
    end
  end
  alarm_tick()
  time.Sleep(0.01) -- Just let it run flat out?
end


Jan 25th, '12, 18:07
Profile
Site Moderator
User avatar

Joined: Aug 18th, '09, 03:32
Posts: 1201
Post Re: input.VK_LCONTROL and input.VK_RCONTROL
Did you try input.VK_CONTROL?

I did some tests.
1st one:
Code:
function printf(...) io.write(string.format(...)) end

while(true) do
   printf("%d %d %d\n", input.KeyGetState(input.VK_CONTROL), input.KeyGetState(input.VK_LCONTROL), input.KeyGetState(input.VK_RCONTROL))
   time.Sleep(0.5)
end

With this if you hold various CTRL keys down you will see the numbers change.
And you will see VK_CONTROL will be set if either the left or right is held down.

This actually turned up something weird with the VK_LCONTROLand VK_RCONTROL keys.
MM uses "GetAsyncKeyState()" and "GetKeyState()" internally.
For which apparently the behavior is different from just VK_RCONTROL.
It flags both key "down" and "was pressed" at the same time.
That's why the test above shows "2" when you hold the key down when it would be preferred for it to be "1" (like VK_CONTROL does).
I made it so the "key down" takes precedence for the next MM update.
Anyhow, this isn't your problem.


Now another test to look at hold and release states:
Code:
function printf(...) io.write(string.format(...)) end

function ShowStates(Prefix)
   printf("%s %d %d %d\n", Prefix, input.KeyGetState(input.VK_CONTROL), input.KeyGetState(input.VK_LCONTROL), input.KeyGetState(input.VK_RCONTROL))
end

-- Test VK_CONTROL hold and release
ShowStates("A1")
input.KeyHold(input.VK_CONTROL)
ShowStates("B1")
input.KeyRelease(input.VK_CONTROL)
ShowStates("C1")

-- Test VK_LCONTROL hold and release
ShowStates("A2")
input.KeyHold(input.VK_LCONTROL)
ShowStates("B2")
input.KeyRelease(input.VK_LCONTROL)
ShowStates("C2")

-- Test VK_RCONTROL hold and release
ShowStates("A3")
input.KeyHold(input.VK_RCONTROL)
ShowStates("B3")
input.KeyRelease(input.VK_RCONTROL)
ShowStates("C3")

Works as expected, except for the case it appears VK_CONTROL and VK_LCONTROL are sort of tied together as the state is both down, while VK_RCONTROL is not.

Although this is different from your case as this is using the gobal way, not the window handle way.

Please try using input.VK_CONTROL and see if that works.


Jan 28th, '12, 14:42
Profile

Joined: Jan 17th, '12, 06:03
Posts: 3
Post Re: input.VK_LCONTROL and input.VK_RCONTROL
Thanks Sirmabus.

Yep I tried all the *CONTROL virtual key variations with no luck.


Jan 30th, '12, 22:20
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 5 posts ] 

Who is online

Users browsing this forum: No registered users and 0 guests


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 post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Based on design by STSoftware, modded by Sirmabus Copyright© 2009-2011