ethanhs / pyhooked

Pure Python hotkey hook, with thanks to pyHook and pyhk
155 stars 28 forks source link

On 64bit Python, lParam[0] and lParam[1] returning large values #6

Closed simon2x closed 8 years ago

simon2x commented 8 years ago

I have been experimenting with this code. The shortcut key never works because the key_code, scan_code (lParam[0] and lParam[1] respectively) returns large values, therefore not in the keycode list.

For example, if I change the loop to just print the event

for h in self.handlers:
    #h(event)
    print(event)

It will return large values.

KeyEvents(event_type='key down', key_code=373662154874, scan_code=953132227430973456, alt_pressed=False, time=223783752) KeyEvents(event_type='key up', key_code=373662154874, scan_code=953132227430973584, alt_pressed=False, time=223783752)

ethanhs commented 8 years ago

Out of curiosity, are you using a standard US keyboard? Other keyboards may have very different scan codes (I think).

simon2x commented 8 years ago

I'm using a UK keyboard. The same code works (i.e. detects the correct scan code) with python 2.7.

bearqq commented 8 years ago

I have the same problem with python 2.7 x64, but it works with 2.7 x86. So I think this is the problem with x64.

ethanhs commented 8 years ago

Can @swprojects can you tell me the version of Python 3 you tested this on? Also, what bitness of 2.7 you tested?

EDIT: Furthermore, I can not reproduce this on Python 3.5 x86, so I am going to hazard a guess that @bearqq is correct that this is a bitness difference. I will wait for the word on bitness. I am not sure why the scancode would report differently on x64, but I will investigate this.

simon2x commented 8 years ago

ah yes. I am using it on python 2.7 x86 and python 3.5 x64

ethanhs commented 8 years ago

Okay, I will try to figure out why these values are so different. Hopefully, there is just an offset, but I might need to do some thinking on this.

ethanhs commented 8 years ago

So, I am really not sure why, but each time you press the key, the scan code increases. I am getting similar behavior, but I'll look at the keycode and see if I can (should) use that instead.

Looks like the key codes are consistent. For 0.8 I will re-write to use keycodes instead of scancodes.

simon2x commented 8 years ago

I have recorded most, but not all of the keycodes ( because of lack of buttons on my keyboard ). Hopefully, the values of keycodes are consistent with yours.

Any values I haven't tested are commented out in the dictionary.


ID_TO_KEY = {
        60129542152: 'Back',
        64424509449: 'Tab',
        120259084301: 'Return',
        249108103188: 'Capital',
        4294967323: 'Escape',
        244813135904: 'Space',
        #33: 'Prior',
        #34: 'Next',
        339302416419: 'End',
        304942678052: 'Home',
        322122547237: 'Left',
        309237645350: 'Up',
        330712481831: 'Right',
        343597383720: 'Down',
        360777252908: 'Snapshot', #PRTSCR
        356482285614: 'Delete',
        47244640304: '0',
        8589934641: '1',
        12884901938: '2',
        17179869235: '3',
        21474836532: '4',
        25769803829: '5',
        30064771126: '6',
        34359738423: '7',
        38654705720: '8',
        42949673017: '9',
        128849018945: 'A',
        206158430274: 'B',
        197568495683: 'C',
        137438953540: 'D',
        77309411397: 'E',
        141733920838: 'F',
        146028888135: 'G',
        150323855432: 'H',
        98784247881: 'I',
        154618822730: 'J',
        158913790027: 'K',
        163208757324: 'L',
        214748364877: 'M',
        210453397582: 'N',
        103079215183: 'O',
        107374182480: 'P',
        68719476817: 'Q',
        81604378706: 'R',
        133143986259: 'S',
        85899346004: 'T',
        94489280597: 'U',
        201863462998: 'V',
        73014444119: 'W',
        193273528408: 'X',
        90194313305: 'Y',
        188978561114: 'Z',
        390842024027: 'Lwin',
        # 92: 'Rwin',
        # 93: 'App',
        # 95: 'Sleep',
        352187318368: 'Numpad0',
        339302416481: 'Numpad1',
        343597383778: 'Numpad2',
        347892351075: 'Numpad3',
        322122547300: 'Numpad4',
        326417514597: 'Numpad5',
        330712481894: 'Numpad6',
        304942678119: 'Numpad7',
        309237645416: 'Numpad8',
        313532612713: 'Numpad9',
        236223201386: 'Multiply',
        335007449195: 'Add',
        317827580013: 'Subtract',
        356482285678: 'Decimal',
        227633266799: 'Divide',
        253403070576: 'F1',
        257698037873: 'F2',
        261993005170: 'F3',
        266287972467: 'F4',
        270582939764: 'F5',
        274877907061: 'F6',
        279172874358: 'F7',
        283467841655: 'F8',
        287762808952: 'F9',
        292057776249: 'F10',
        373662154874: 'F11',
        377957122171: 'F12',
        296352743568 : 'Numlock',
        180388626592 : 'Lshift',
        231928234145 : 'Rshift',
        124554051746 : 'Lcontrol',
        124554051747: 'Rcontrol',
        # 164: 'Lmenu',
        # 165: 'Rmenu',
        # 186: 'Oem_1',
        # 187: 'Oem_Plus',
        # 188: 'Oem_Comma',
        # 189: 'Oem_Minus',
        # 190: 'Oem_Period',
        # 191: 'Oem_2',
        # 192: 'Oem_3',
        # 219: 'Oem_4',
        # 220: 'Oem_5',
        # 221: 'Oem_6',
        # 222: 'Oem_7',
        # 1001: 'mouse left',  # mouse hotkeys
        # 1002: 'mouse right',
        # 1003: 'mouse middle',
        # 1000: 'mouse move',  # single event hotkeys
        # 1004: 'mouse wheel up',
        # 1005: 'mouse wheel down',
        124554051746: 'Ctrl',  # merged hotkeys
        240518168740: 'Alt',
        180388626592: 'Shift',
        390842024027: 'Win'
        }
ethanhs commented 8 years ago

Thanks @swprojects those do seem to correspond with the key codes I am getting. I was about to generate that list this morning, but you saved me the work :) I will try to fill in the gaps later.

panofish commented 8 years ago

I am also using 64 bit Python 2.7 and I am getting this error when testing with demo program and pressing a key.

...line 46, in print_event scancode=int(str(e)[(start+10):end])

ValueError: invalid literal for int() with base 10: '101567335076200448L'

ethanhs commented 8 years ago

@swprojects I'm afraid I don't really have time to work this out at the moment. There is the beginnings of a fix in the dev branch, and if you wish to fix this for x64 and contribute back it would be appreciated, otherwise, it may be a while.

panofish commented 8 years ago

OK thanks ... your original sharing was much appreciated. I will patiently check back once in while.

Thanks.

Alan Lilly

On Sun, Jul 24, 2016 at 4:47 PM, Ethan notifications@github.com wrote:

@swprojects https://github.com/swprojects I'm afraid I don't really have time to work this out at the moment. There is the beginnings of a fix in the dev branch, and if you wish to fix this for x64 and contribute back it would be appreciated, otherwise, it may be a while.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ethanhs/pyhooked/issues/6#issuecomment-234801277, or mute the thread https://github.com/notifications/unsubscribe-auth/AC7CWPhxykw-333aNi7BVnwTogHiSwdkks5qY89dgaJpZM4JKdV7 .

simon2x commented 8 years ago

@ethanhs thanks very much for this, looks much cleaner. I will try to help with the x64 fix

@panofish and a fix for the GUI mainloop (if possible)

I have forked this project, which I will try to dedicate some time to work on dev

fork: https://github.com/swprojects/pyhooked

ethanhs commented 8 years ago

This is fixed in c070a01a7f7ea5daba4826a914fbee7bcb7aabff and above (currently in the dev branch). Please uninstall previous versions before installing again with pip or setup.py.