PDA

View Full Version : KeyDown not working



riwu
01-28-2017, 09:23 AM
begin
KeyDown(VK_1);
wait(2000);
KeyUp(VK_1);
end.

The above code outputs only a single '1' keystroke.

Does anyone get multiple 1's from running this? (Just open Simba and run it, no SMART etc)

deejaay
01-28-2017, 10:31 AM
Nope only got a single 1, guess you'll have to do a repeat until for inputting multiple.

acow
01-28-2017, 01:41 PM
The key does seem to be down the whole time, but isn't currently spamming inputs like holding a key down on a normal keyboard would. It does however spam inputs if used on SMART.

slacky
01-28-2017, 11:47 PM
I am not sure WHY it's acting like this, but this behavior is inherited from the functions in Windows: both SendInput and keybd_event acts this way. It's not a Simba specific "issue", any program using those windows functions will be forced to use a loop to repeat a key it seems.

Borland
01-29-2017, 10:49 AM
Repeated keystrokes from holding a key down is a relationship between the OS and the keyboards controller. When we send keys through simba we are bypassing that controller. So we have to act on it's behalf.

When a key is held, the default wait time before the key is classed as being "held" (rather than pressed slowly) is 500ms. Then the default Windows repeat is every 33ms (this can be changed in keyboard settings but it's very rare anyone has).

So the following will simulate the default scenario of a key being held.


KeyDown(VK_1);
Wait(500);
KeyUp(VK_1);

for 1 to 9 do
begin
KeyDown(VK_1);
KeyUp(VK_1);
Wait(33);
end;


Now, I've never checked whether the wait occurs between the key up/down or after. For some reason I think it'd be done after, so that's what I've done above.

The code above will repeat a key as though we wanted it inputted 10 times. Of course if you want the key held for a certain length of time, use a while loop on a timer. If this is for RS purposes, don't forget some random on that timer :D

Edit:

Another fun thing about this issue is that on RS the following

KeyDown(VK_RIGHT);
wait(2000);
KeyUp(VK_RIGHT);
will actually turn the camera for the time stated. This is because how the game handles camera inputs is different than how usual keyboard input is handled.

BlitzKrieger
02-01-2017, 09:10 PM
I actually just wrote something as I noticed this the other day as well while trying to write a script for FF2. I ended up with the following


begin
repeat
if (exec) > 100 then
break;
inc(exec);
sendKeys('x',1200,50); \\(presses 'x' 9 times)
wait(200);
sendKeys('y',800,50); \\ (presses 'y' 4-5 times)
writeln((exec));
until false;
end;