Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting a keypress in python while in the background

Tags:

python

I am trying to find a way to detect a keypress and then run a method depending on what key it is.

I can already do this with Tkinter. But what I can't do is detect the keypress while the window is in the background. I will be running this program in the background while I play a game. I need it to be able to detect inputs while I'm in the game.

Is there any way I can do this with Tkinter or something else? Preferably I would like to not have to download anything external as I would like to distribute this to some other people.

like image 484
Slidedrum Avatar asked Nov 23 '15 04:11

Slidedrum


People also ask

How do I track pressed keys in Python?

To detect keypress, we will use the is_pressed() function defined in the keyboard module. The is_pressed() takes a character as input and returns True if the key with the same character is pressed on the keyboard.

How do you check if user pressed Enter in Python?

user_input=input("ENTER SOME POSITIVE INTEGER : ") if((not user_input) or (int(user_input)<=0)): print("ENTER SOME POSITIVE INTEGER GREATER THAN ZERO") #print some info import sys #import sys. exit(0) #exit program ''' #(not user_input) checks if user has pressed enter key without entering # number.

How does Python detect multiple key presses?

You could try using and : while keypressed('w') and keypressed('a'): . As for detecting if the key is released, like the keypressed , you can use the keyrelease .

How do you wait for a keypress in Python?

In Python 2 use raw_input(): raw_input("Press Enter to continue...") This only waits for the user to press enter though. This should wait for a keypress.


2 Answers

Too get all the properties from your key press event. you can do the following

import pythoncom, pyHook

def OnKeyboardEvent(event):
    print('MessageName:',event.MessageName)
    print('Message:',event.Message)
    print('Time:',event.Time)
    print('Window:',event.Window)
    print('WindowName:',event.WindowName)
    print('Ascii:', event.Ascii, chr(event.Ascii))
    print('Key:', event.Key)
    print('KeyID:', event.KeyID)
    print('ScanCode:', event.ScanCode)
    print('Extended:', event.Extended)
    print('Injected:', event.Injected)
    print('Alt', event.Alt)
    print('Transition', event.Transition)
    print('---')

# return True to pass the event to other handlers
    return True

# create a hook manager
hm = pyHook.HookManager()
# watch for all mouse events
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()

Now know all the details of the key press and do operation on top of this.

pressing 's' would look like this

MessageName: key down
Message: 256
Time: 449145375
Window: 2558060
WindowName: "file name"
Ascii: 115 s
Key: S
KeyID: 83
ScanCode: 31
Extended: 0
Injected: 0
Alt 0
Transition 0
like image 68
Shuvendu Roy Avatar answered Oct 26 '22 18:10

Shuvendu Roy


pyHook seems like it would work well for this (mentioned by furas)

from pyHook import HookManager
from win32gui import PumpMessages, PostQuitMessage

class Keystroke_Watcher(object):
    def __init__(self):
        self.hm = HookManager()
        self.hm.KeyDown = self.on_keyboard_event
        self.hm.HookKeyboard()


    def on_keyboard_event(self, event):
        try:
            if event.KeyID  == keycode_youre_looking_for:
                self.your_method()
        finally:
            return True

    def your_method(self):
        pass

    def shutdown(self):
        PostQuitMessage(0)
        self.hm.UnhookKeyboard()


watcher = Keystroke_Watcher()
PumpMessages()
like image 28
user2682863 Avatar answered Oct 26 '22 20:10

user2682863