0

From what I've seen, most issues consist of sending in a keystroke

I need to be able to emulate pressing and raising keys on a lower level so that programs actually think these keys are being pressed

pseudo-code

Press("A")
#do something else
Unpress("A")

Does anybody know if this is possible? Even if it needs more than just native Python, I'm willing to try as long as the solution doesn't have a large amount of latency between emulating the key and the actual key

QFSW
  • 100
  • 1
  • 12
  • Possible duplicate of [Python simulate keydown](http://stackoverflow.com/questions/11906925/python-simulate-keydown) – shad0w_wa1k3r Mar 19 '17 at 10:12
  • From what I can see it works for pressing, but (correct me if I'm wrong) I can't tell from there how to press and hold? – QFSW Mar 19 '17 at 10:19
  • Well, keydown means the key is down untill there is a keyup. – shad0w_wa1k3r Mar 19 '17 at 10:21
  • I can see that `SendInput(Keyboard(KEY_A, KEYEVENTF_KEYUP))` is possible, but I can't find any `KEYDOWN` equivalent – QFSW Mar 19 '17 at 10:26
  • I am guessing here that `KEYEVENTF_KEYUP` should be similar to `KEYEVENTF_KEYDOWN`, but you'll have to check for yourself. – shad0w_wa1k3r Mar 19 '17 at 10:30
  • `KEYEVENTF_KEYDOWN` isn't defined, intuitively I thought it would be as you did – QFSW Mar 19 '17 at 10:31
  • Declaring `KEYEVENTF_KEYDOWN = 0x0000` before the line `KEYEVENTF_KEYUP = 0x0002` should work. – shad0w_wa1k3r Mar 19 '17 at 10:34
  • Still only presses it once instead of holding it down – QFSW Mar 19 '17 at 10:38
  • Oh yes, you should then have to write a small loop that executes the keydown event over a reasonable interval to "keep pressing / holding down". – shad0w_wa1k3r Mar 19 '17 at 10:46
  • Could work but not sure if it would be particularly reliable – QFSW Mar 19 '17 at 10:48
  • Like I've mentioned, sending a `KEYDOWN` should be sufficient for a program to note that they key is held down, unless we also send a `KEYUP` later. The reliability you are talking about, really depends on the program for which you are simulating the keystrokes. – shad0w_wa1k3r Mar 19 '17 at 10:55
  • Except from what I've tested, KEYDOWN does not keep the key pressed down but merely sends a single instance of the keystroke – QFSW Mar 19 '17 at 10:57
  • It's a hardware signal. Key down tells that key is down. Key up tells the key is up. When you press a key, say "a", you send the Key down first & then the Key up (if you release the key). The program / editor is the one that checks whether a Key up was received, else, it starts repeating the "a" after a brief pause, at regular intervals, until it sees a Key up. It never expects a Key down to be continuously triggered. – shad0w_wa1k3r Mar 19 '17 at 11:00
  • So then running `SendInput(Keyboard(KEY_A, KEYEVENTF_KEYDOWN))` should keep the A key down indefinitely since I never called KEYUP, correct? If i do this it doesn't do that, it only sends a single keystroke of A – QFSW Mar 19 '17 at 11:04
  • Yes, but like I said, it is upto the program to handle the nature of the key events. So, in your case, that might not be. Which is why you will have to resort to the "repeat keystroke over a sufficient interval". I see no other possibility. – shad0w_wa1k3r Mar 19 '17 at 11:06
  • All I want is the same functionality as pressing and holding the key on a real keyboard, which this code is not doing. It's not a fault of the program handling the keys as it works when I do it on my keyboard, just not from code – QFSW Mar 19 '17 at 11:12
  • Yeah, my bad. I once used Key down instead of Key up, for certain reasons and since thought that Key up was necessary, but it isn't, as seen from a few SO answers / comments that I came across. You will have to simulate the periodic Key down repetition if you want the "hold" behaviour. Related - https://stackoverflow.com/questions/11355595/is-it-possible-to-override-the-keydown-repeat-delay-in-javascript Also, you can hop onto the Python chatroom to get more clarity from others. – shad0w_wa1k3r Mar 19 '17 at 11:20

1 Answers1

0
from pynput.keyboard import Key, Controller

keyboard = Controller()

# Type a lower case A; this will work even if no key on the
# physical keyboard is labelled 'A'
keyboard.press('a')
keyboard.release('a')

You can find nore info here: https://pypi.python.org/pypi/pynput

dnogin
  • 21
  • 1