July 2002
Intermediate to advanced
608 pages
15h 46m
English
Credit: Bill Bell
You need to display the hotkeys associated with Windows shortcuts as readable key-combination names, such as Alt-Ctrl-Z or Shift-Ctrl-B.
Key combinations are returned as binary-coded numbers, but it’s not hard to decode them into human-readable combinations:
import sys
# Append to sys.path the complete path to the folder that contains
# 'link.py' (the demo use of pythoncom.CoCreateInstance with
# shell.CLSID_ShellLink in the ActiveState distribution of Python)
# so that link can be imported as a module
sys.path.append('C:/Python21/win32comext/shell/test')
import link
import commctrl
class PyShortcut_II(link.PyShortcut):
def decode_hotkey(self):
hk = self.GetHotkey( )
result = ''
if hk:
mod = hk >> 8
if mod & commctrl.HOTKEYF_SHIFT: result += 'Shift-'
if mod & commctrl.HOTKEYF_CONTROL: result += 'Control-'
if mod & commctrl.HOTKEYF_ALT: result += 'Alt-'
result += chr ( hk % 256 )
return resultHere’s a typical usage pattern:
>>> shortcut = PyShortcut_II( )
>>> shortcut.load(r'C:\WINDOWS\DESKTOP\Pygris.lnk' )
>>> shortcut.decode_hotkey( )
'Control-Alt-T'The ActiveState Python distribution includes an example that shows how to get and set Win32 shortcuts. This recipe shows how to extend the example to decode the hotkeys (such as Alt-Ctrl-Z and Shift-Ctrl-B) that can be associated with shortcuts.
On Win32, each shortcut can have an associated hotkey. In the
link.py that is distributed as ...
Read now
Unlock full access