+ 9
Using GetAsyncKeyState in Ruby
How can I use GetAsyncKeyState in Ruby, does anyone know? All I know is you can get it by requiring Win32API but how can I use the GetAsyncKeyState, reply please ASAP EDIT: I tried using Linux to see if this works but it said an error constant dynamic assignment Here's the code I saw online(Modified a bit): require 'Win32API' GetAsyncKeyState = Win32API.new("user32", "GetAsyncKeyState", ['i'], 'i') GetAsyncKeyState.call(0x58) if GetAsyncKeyState.call(0x58) & 0x01 == 1 puts "X pressed!" end puts "Working?"
10 Answers
+ 1
I'm not too sure about Linux.
Using a capital as the variable name makes it const.
Also you did a call twice. The least significant bit checks if the key was pressed since the last call and then set to 0. So your second call will just read 0 all the time. Even if you did not do 2 calls another process may have called GetAsyncKeyState. The most significant bit is set when the key is held down so you should read that:
require 'WIN32API'
getAsyncKeyState = Win32API.new("User32", "GetAsyncKeyState", ['i'], 'i')
if getAsyncKeyState.call(0x58) & 0x8000 == 32768
puts "X pressed"
end
+ 8
I've finished a small game using Win32 Sync Keys :D thanks man:D
+ 6
How can I know 0x58 is 'x' ? Is it a hexadecimal value? and from that code will it work? In Linux and Windows? What does 32768 and 0x8000 mean?
+ 6
What if I want a specific button to be pressed for example I want the user to enter letter F? Can you give me a little code example?
+ 6
Oh wait nevermind
+ 6
By which my 2 questions is:
1: Do I need WIN32API to be all uppercase or is it optional?
2: Can I use 1 instead of 32768?
+ 6
@Dennis Thanks man, very appreciated! :D Without you I can't translate my CPP game into Ruby :D
+ 6
Hey @Dennis I have a problem with my code:
I tried getting holding down a key and it should do something but it didnt I was going to move something if UP button was pressed:
if getAsyncKeyState.call(0x26) & 0x8000 == 32768
y2 = (y - 1)
cade map[y2][x]
when ' '
map[y][x] = ' '
y -= 1
map[y2][x] = '@'
end
end
Is something wrong?
+ 6
I posted mine too, feel free to edit it because it creates an error
+ 1
https://msdn.microsoft.com/en-us/library/windows/desktop/dd375731(v=vs.85).aspx
The code runs on windows at least, I don't use linux so I don't know.
0x8000 is just the way to read the most significant bits and 32768 is the value you end up with if the button is held down.