PyHook doesn't detect key pressed in some windows
Asked Answered
C

4

14

I ran this PyHook sample code:

    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()

To try it out and it works in most windows but when I try it inside a game window it acts like I'm not pressing anything.

Is there a way to listen to keypresses from a specific process? what should I do?

Corvin answered 27/10, 2015 at 20:23 Comment(0)
C
2

I am quoting from the same page from where you got this code. Check Here.

If you are using a GUI toolkit (e.g. wxPython), this loop is unnecessary since the toolkit provides its own.

I think you should change pythoncom.PumpMessages() to something else I don't know what.

Combe answered 12/4, 2016 at 16:41 Comment(0)
O
2

Try using keyboard instead here is an example according to the keyboard page on Github. You have to run this with sudo through the command (in command prompt) sudo python file.py

import sys
sys.path.append('..')
import keyboard
def print_pressed_keys(e):
    line = ', '.join(str(code) for code in keyboard._pressed_events)
    # '\r' and end='' overwrites the previous line.
    # ' '*40 prints 40 spaces at the end to ensure the previous line is cleared.
    print('\r' + line + ' ' * 40k, end='')


keyboard.hook(print_pressed_keys)
keyboard.wait()
Opprobrious answered 28/5, 2019 at 11:39 Comment(1)
This is also global.Opprobrious
D
2

Games often use hooks to handle input in much the same way as you are trying to, the way that hooks work under windows is as a chain, IIRC the last hook added is the first hook called, so it may be that if you are starting your script before the game then the games hook is called before yours, handles the event and so nothing reaches your hook.

Another possibility is that to prevent people for scripting/automating games or otherwise making things to help the player games will re-register the hooks periodically to ensure it is always at the head of the hook chain, if this is the case then you will find it difficult to overcome.

Diplomatist answered 29/5, 2019 at 10:3 Comment(0)
K
1

Your problem might be caused by the fact that your computer is giving the game priority over your keypress program, so when you press a key, the input goes directly to your game instead of through your program.

EDIT:

Actually, there is a way to do this. If you go into press ctrl+alt+del and select task manager (windows), you should be able to see the name of your python program. Now, right click on that and select Go to details. This should change the format completely. Without clicking anything else, right click on the highlighted thing (should be highlighted in blue) and hover over Set priority. This should bring up a dropdown menu, where you can either set as High or Realtime.

Another way to make sure to do this is to set your game's priority to Below Normal using the same steps, but click on the game instead.

Klaxon answered 29/5, 2019 at 0:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.