Welcome, guest | Sign In | My Account | Store | Cart

The ActiveState Python distribution includes an example that shows how to get and set MSW shortcuts. This code 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.

Python, 27 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
PythonWin 2.1 (#15, Jun 18 2001, 21:42:28) [MSC 32 bit (Intel)] on win32.
Portions Copyright 1994-2001 Mark Hammond (MarkH@ActiveState.com) - see 'Help/About PythonWin' for further copyright information.
>>> import sys
>>> # append the complete name of the folder that contains 'link.py'
>>> # (the demo use of pythoncom.CoCreateInstance with shell.CLSID_ShellLink
>>> # in the ActiveState distribution of Python)
>>> # on your computer to sys.path (to make 'link.py' accessible)
>>> #
>>> sys.path.append ( r'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 result
... 	
>>> shortcut = PyShortcut_II ( )
>>> shortcut.load ( r'C:\WINDOWS\DESKTOP\Pygris.lnk' )
>>> shortcut.decode_hotkey ( )
'Control-Alt-T'
Created by Bill Bell on Tue, 4 Sep 2001 (PSF)
Python recipes (4591)
Bill Bell's recipes (16)
Python Cookbook Edition 1 (103)

Required Modules

  • (none specified)

Other Information and Tasks