PYHK – Python Hotkey Module

I used to program quite a bit using AutoHotKey to automate several tasks in Windows. Its pretty easy to use and as long as you dont need to do anything too complicated it does the job. But as soon as you need to do something more than just move around a window, or sending a hotkey, you need a more powerful tool. This is when I developed pyhk – a python hotkey module.

At some point I was looking to use some hotkeys for some advanced programming. It seemed appropriate to use pythonW for that. I found pywinautoW fairly quickly and immediately fell in love with it. The only thing missing were global hotkeys. The best module I could find was pyhookW. I somehow couldnt find a way to just register simple hotkeys. I feel like I reinvented the wheel and invented a program to register hotkeys and trigger functions with it. I called it pyhk to honor its main source pyhookW and to attribute the inspiration from autohotkeyW.

Functionality

You can register any sequence of keys with any function you like. This will create a global hotkey used in all running programs on your Windows machine.

Example

import pyhk

def fun():
    print "Do something"

#create pyhk class instance
hot = pyhk.pyhk()

#add hotkey
hot.addHotkey(['Ctrl', 'Alt','7'],fun)

#start looking for hotkey.
hot.start()

Documentation

Please see the end user documentation for more details. It contains various examples and solutions for common problems. Using hotkeys in python should be simple. It is.

Python version

2.6

Python modules required

pyhook, pywin32

License

GNU GENERAL PUBLIC LICENSE, feel free to use and share it :)

Download

Want to stay up to date with new releases? Subscribe to the right. Are you using PYHK? Let me know what you think about it in the comments below. I am always open to new ideas and ways to improve.

Current version

Log pyhk

pyhk0.36

– bug fix in isIDHotkey

pyhk0.35

– execute single keys on KeyUpEvent with addHotkey( … , up = True)

pyhk0.34

– return id on addHotkey registration

– enable removeHotkey(id = id)

– removeHotkey() removes al hotkeys

pyhk0.33

– isThread option at addHotkey

 pyhk0.32

– add getHotkeyListNoSingleNoModifiers function to return list of hotkeys

– set endHotkey with merged hotkeys (i.e. change endHotkey from ['Lcontrol','Lshift','Q'] to ['Ctrl','Shift','Q'])

– changed endHotkey loop (just made it a normal hotkey)

pyhk0.30

– Mouse hotkeys. Example: ['mouse left','A'] is now possible as a hotkey

– Single use hotkeys for mouse move, mouse wheel up and mouse wheel down. They can not be combined with any other hotkey. Example use: ['mouse wheel up']

– Easier hotkey registration for Ctrl, Alt, Shift: ['Ctrl','7'] is equal to ['Lcontrol','7'] and ['Rcontrol','7']

The Top 8 WordPress Developer Tools from 5 years experience
(Hint: They cut down my development time in less than half!)

Get the Top Tools Now!

Comments

  1. Ruso says

    Cannot import pyhk. It is giving the following error: ImportError: No module named pythoncom

    Can you please help me with it.

    • admin says

      Hi Mike,

      sorry for the late response.

      PYHK only depends on Pyhook. To my knowledge they did not port Pyhook to 3.0
      thus I cant port mine either.

      If you get them to port should be a matter of a few minutes to adjust PYHK.

  2. DavidA says

    Hey, Would be good if you made a module which auto-detects OS and relays to relevant OS-specific hotkey module.

    Basically make this compatible with all OSs

    • admin says

      I agree that this is definitely missing, however, unfortunately I dont have the time to do this right now.

  3. John says

    Will there be any inclusion of keys like “PrtScr” or “PgUp”? I am trying to make a program that takes a screen-shot when a user presses win+prtscr. But all around a great program. Also, it doesn’t really support pip :( It would be great if it were to support pip and the other keys I mentioned. Thanks for this great script!

Leave a Reply

Your email address will not be published. Required fields are marked *