How to Send F6 with Only a Number Pad (Stupid Python Tricks)
Friday, December 22nd, 2006This little script will detect when you hit certain number pad numbers and send the specified function keys on to Windows instead:
import win32com.client
import pythoncom
import pyHook # http://sourceforge.net/projects/uncassist
def translate_keystrokes(event):
if event.Key=='Numpad6': #Next in Screen Saver
print 'You hit numpad 6!'
shell.SendKeys("{F7}")
return False #blocks keystroke
elif event.Key=='Numpad4': #Previous in Screen Saver
print 'You hit numpad 4!'
shell.SendKeys("{F6}")
return False #blocks keystroke
else:
return True #Key stroke continues to Windows apps
#object to let us send keystrokes
shell = win32com.client.Dispatch("WScript.Shell")
# create a hook manager
hm = pyHook.HookManager()
# watch for all keyboard events
hm.KeyDown = translate_keystrokes
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()
Why on earth would I want to do this?
Well, this lets me control a screensaver application with a $10 number pad so that the laptop controlling the display can be tucked away out of site. The alternative was to buy this $80 programmable keypad. (If you hadn’t guessed, the screen saver inflexibly requires F6 to move to the next page.)
To make this little script I followed the pyHook examples here. And figured out how to send keystrokes here.
Finally this page gives you a run down on what WScript.Shell does. And this project looks like another promising alternative for sending keystrokes.