Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在PyWin32中获取密钥的名称并给出其密钥代码?_Python_Python 3.x_Key_Pywin32 - Fatal编程技术网

Python 如何在PyWin32中获取密钥的名称并给出其密钥代码?

Python 如何在PyWin32中获取密钥的名称并给出其密钥代码?,python,python-3.x,key,pywin32,Python,Python 3.x,Key,Pywin32,我正在读,不知为什么,那本书不在那里。无法使用或返回名称,因为它们显然只返回状态。那么,为什么GetKeyNameText不在那里,如何获得一个键的名称并给出它的键码(从0到256) 例如: import win32api if __name__ == "__main__": while True: for key in range(256): if int(win32api.GetKeyState(key)):

我正在读,不知为什么,那本书不在那里。无法使用或返回名称,因为它们显然只返回状态。那么,为什么GetKeyNameText不在那里,如何获得一个键的名称并给出它的键码(从0到256)

例如:

import win32api

if __name__ == "__main__":

    while True:
        for key in range(256):
            if int(win32api.GetKeyState(key)):
                print(win32api.GetKeyNameText(key)) # Not available in PyWin32.
输出:

Traceback (most recent call last):
  File "main.py", line 32, in <module>
    print(win32api.GetKeyNameText(key)) # Not available in Python.
AttributeError: 'module' object has no attribute 'GetKeyNameText'
Press any key to continue . . .
回溯(最近一次呼叫最后一次):
文件“main.py”,第32行,在
打印(win32api.GetKeyNameText(key))#在Python中不可用。
AttributeError:“模块”对象没有属性“GetKeyNameText”
按任意键继续。

对于密钥,您很可能需要为密钥创建一个VK_代码字典,例如,虚拟密钥代码-

VK_CODE = {8: 'backspace',
 9: 'tab',
 12: 'clear',
 13: 'enter',
 16: 'shift',
 17: 'ctrl',
 18: 'alt',
 19: 'pause',
 20: 'caps_lock',
 27: 'esc',
 32: 'spacebar',
 33: 'page_up',
 34: 'page_down',
 35: 'end',
 36: 'home',
 37: 'left_arrow',
 38: 'up_arrow',
 39: 'right_arrow',
 40: 'down_arrow',
 41: 'select',
 42: 'print',
 43: 'execute',
 44: 'print_screen',
 45: 'ins',
 46: 'del',
 47: 'help',
 48: '0',
 49: '1',
 50: '2',
 51: '3',
 52: '4',
 53: '5',
 54: '6',
 55: '7',
 56: '8',
 57: '9',
 65: 'a',
 66: 'b',
 67: 'c',
 68: 'd',
 69: 'e',
 70: 'f',
 71: 'g',
 72: 'h',
 73: 'i',
 74: 'j',
 75: 'k',
 76: 'l',
 77: 'm',
 78: 'n',
 79: 'o',
 80: 'p',
 81: 'q',
 82: 'r',
 83: 's',
 84: 't',
 85: 'u',
 86: 'v',
 87: 'w',
 88: 'x',
 89: 'y',
 90: 'z',
 96: 'numpad_0',
 97: 'numpad_1',
 98: 'numpad_2',
 99: 'numpad_3',
 100: 'numpad_4',
 101: 'numpad_5',
 102: 'numpad_6',
 103: 'numpad_7',
 104: 'numpad_8',
 105: 'numpad_9',
 106: 'multiply_key',
 107: 'add_key',
 108: 'separator_key',
 109: 'subtract_key',
 110: 'decimal_key',
 111: 'divide_key',
 112: 'F1',
 113: 'F2',
 114: 'F3',
 115: 'F4',
 116: 'F5',
 117: 'F6',
 118: 'F7',
 119: 'F8',
 120: 'F9',
 121: 'F10',
 122: 'F11',
 123: 'F12',
 124: 'F13',
 125: 'F14',
 126: 'F15',
 127: 'F16',
 128: 'F17',
 129: 'F18',
 130: 'F19',
 131: 'F20',
 132: 'F21',
 133: 'F22',
 134: 'F23',
 135: 'F24',
 144: 'num_lock',
 145: 'scroll_lock',
 160: 'left_shift',
 161: 'right_shift ',
 162: 'left_control',
 163: 'right_control',
 164: 'left_menu',
 165: 'right_menu',
 166: 'browser_back',
 167: 'browser_forward',
 168: 'browser_refresh',
 169: 'browser_stop',
 170: 'browser_search',
 171: 'browser_favorites',
 172: 'browser_start_and_home',
 173: 'volume_mute',
 174: 'volume_Down',
 175: 'volume_up',
 176: 'next_track',
 177: 'previous_track',
 178: 'stop_media',
 179: 'play/pause_media',
 180: 'start_mail',
 181: 'select_media',
 182: 'start_application_1',
 183: 'start_application_2',
 186: ';',
 187: '+',
 188: ',',
 189: '-',
 190: '.',
 191: '/',
 192: '`',
 219: '[',
 220: '\\',
 221: ']',
 222: "'",
 246: 'attn_key',
 247: 'crsel_key',
 248: 'exsel_key',
 250: 'play_key',
 251: 'zoom_key',
 254: 'clear_key'}

另外,您应该对照
-127
检查
int(win32api.GetKeyState(key))
,如果切换键,它可以是
1

对于键,您很可能需要为键创建一个VK_代码字典,例如,虚拟键代码存在-

VK_CODE = {8: 'backspace',
 9: 'tab',
 12: 'clear',
 13: 'enter',
 16: 'shift',
 17: 'ctrl',
 18: 'alt',
 19: 'pause',
 20: 'caps_lock',
 27: 'esc',
 32: 'spacebar',
 33: 'page_up',
 34: 'page_down',
 35: 'end',
 36: 'home',
 37: 'left_arrow',
 38: 'up_arrow',
 39: 'right_arrow',
 40: 'down_arrow',
 41: 'select',
 42: 'print',
 43: 'execute',
 44: 'print_screen',
 45: 'ins',
 46: 'del',
 47: 'help',
 48: '0',
 49: '1',
 50: '2',
 51: '3',
 52: '4',
 53: '5',
 54: '6',
 55: '7',
 56: '8',
 57: '9',
 65: 'a',
 66: 'b',
 67: 'c',
 68: 'd',
 69: 'e',
 70: 'f',
 71: 'g',
 72: 'h',
 73: 'i',
 74: 'j',
 75: 'k',
 76: 'l',
 77: 'm',
 78: 'n',
 79: 'o',
 80: 'p',
 81: 'q',
 82: 'r',
 83: 's',
 84: 't',
 85: 'u',
 86: 'v',
 87: 'w',
 88: 'x',
 89: 'y',
 90: 'z',
 96: 'numpad_0',
 97: 'numpad_1',
 98: 'numpad_2',
 99: 'numpad_3',
 100: 'numpad_4',
 101: 'numpad_5',
 102: 'numpad_6',
 103: 'numpad_7',
 104: 'numpad_8',
 105: 'numpad_9',
 106: 'multiply_key',
 107: 'add_key',
 108: 'separator_key',
 109: 'subtract_key',
 110: 'decimal_key',
 111: 'divide_key',
 112: 'F1',
 113: 'F2',
 114: 'F3',
 115: 'F4',
 116: 'F5',
 117: 'F6',
 118: 'F7',
 119: 'F8',
 120: 'F9',
 121: 'F10',
 122: 'F11',
 123: 'F12',
 124: 'F13',
 125: 'F14',
 126: 'F15',
 127: 'F16',
 128: 'F17',
 129: 'F18',
 130: 'F19',
 131: 'F20',
 132: 'F21',
 133: 'F22',
 134: 'F23',
 135: 'F24',
 144: 'num_lock',
 145: 'scroll_lock',
 160: 'left_shift',
 161: 'right_shift ',
 162: 'left_control',
 163: 'right_control',
 164: 'left_menu',
 165: 'right_menu',
 166: 'browser_back',
 167: 'browser_forward',
 168: 'browser_refresh',
 169: 'browser_stop',
 170: 'browser_search',
 171: 'browser_favorites',
 172: 'browser_start_and_home',
 173: 'volume_mute',
 174: 'volume_Down',
 175: 'volume_up',
 176: 'next_track',
 177: 'previous_track',
 178: 'stop_media',
 179: 'play/pause_media',
 180: 'start_mail',
 181: 'select_media',
 182: 'start_application_1',
 183: 'start_application_2',
 186: ';',
 187: '+',
 188: ',',
 189: '-',
 190: '.',
 191: '/',
 192: '`',
 219: '[',
 220: '\\',
 221: ']',
 222: "'",
 246: 'attn_key',
 247: 'crsel_key',
 248: 'exsel_key',
 250: 'play_key',
 251: 'zoom_key',
 254: 'clear_key'}

另外,您应该对照
-127
检查
int(win32api.GetKeyState(key))
,如果切换键,它可以是
1

对于键,您很可能需要为键创建一个VK_代码字典,例如,虚拟键代码存在-

VK_CODE = {8: 'backspace',
 9: 'tab',
 12: 'clear',
 13: 'enter',
 16: 'shift',
 17: 'ctrl',
 18: 'alt',
 19: 'pause',
 20: 'caps_lock',
 27: 'esc',
 32: 'spacebar',
 33: 'page_up',
 34: 'page_down',
 35: 'end',
 36: 'home',
 37: 'left_arrow',
 38: 'up_arrow',
 39: 'right_arrow',
 40: 'down_arrow',
 41: 'select',
 42: 'print',
 43: 'execute',
 44: 'print_screen',
 45: 'ins',
 46: 'del',
 47: 'help',
 48: '0',
 49: '1',
 50: '2',
 51: '3',
 52: '4',
 53: '5',
 54: '6',
 55: '7',
 56: '8',
 57: '9',
 65: 'a',
 66: 'b',
 67: 'c',
 68: 'd',
 69: 'e',
 70: 'f',
 71: 'g',
 72: 'h',
 73: 'i',
 74: 'j',
 75: 'k',
 76: 'l',
 77: 'm',
 78: 'n',
 79: 'o',
 80: 'p',
 81: 'q',
 82: 'r',
 83: 's',
 84: 't',
 85: 'u',
 86: 'v',
 87: 'w',
 88: 'x',
 89: 'y',
 90: 'z',
 96: 'numpad_0',
 97: 'numpad_1',
 98: 'numpad_2',
 99: 'numpad_3',
 100: 'numpad_4',
 101: 'numpad_5',
 102: 'numpad_6',
 103: 'numpad_7',
 104: 'numpad_8',
 105: 'numpad_9',
 106: 'multiply_key',
 107: 'add_key',
 108: 'separator_key',
 109: 'subtract_key',
 110: 'decimal_key',
 111: 'divide_key',
 112: 'F1',
 113: 'F2',
 114: 'F3',
 115: 'F4',
 116: 'F5',
 117: 'F6',
 118: 'F7',
 119: 'F8',
 120: 'F9',
 121: 'F10',
 122: 'F11',
 123: 'F12',
 124: 'F13',
 125: 'F14',
 126: 'F15',
 127: 'F16',
 128: 'F17',
 129: 'F18',
 130: 'F19',
 131: 'F20',
 132: 'F21',
 133: 'F22',
 134: 'F23',
 135: 'F24',
 144: 'num_lock',
 145: 'scroll_lock',
 160: 'left_shift',
 161: 'right_shift ',
 162: 'left_control',
 163: 'right_control',
 164: 'left_menu',
 165: 'right_menu',
 166: 'browser_back',
 167: 'browser_forward',
 168: 'browser_refresh',
 169: 'browser_stop',
 170: 'browser_search',
 171: 'browser_favorites',
 172: 'browser_start_and_home',
 173: 'volume_mute',
 174: 'volume_Down',
 175: 'volume_up',
 176: 'next_track',
 177: 'previous_track',
 178: 'stop_media',
 179: 'play/pause_media',
 180: 'start_mail',
 181: 'select_media',
 182: 'start_application_1',
 183: 'start_application_2',
 186: ';',
 187: '+',
 188: ',',
 189: '-',
 190: '.',
 191: '/',
 192: '`',
 219: '[',
 220: '\\',
 221: ']',
 222: "'",
 246: 'attn_key',
 247: 'crsel_key',
 248: 'exsel_key',
 250: 'play_key',
 251: 'zoom_key',
 254: 'clear_key'}

另外,您应该对照
-127
检查
int(win32api.GetKeyState(key))
,如果切换键,它可以是
1

对于键,您很可能需要为键创建一个VK_代码字典,例如,虚拟键代码存在-

VK_CODE = {8: 'backspace',
 9: 'tab',
 12: 'clear',
 13: 'enter',
 16: 'shift',
 17: 'ctrl',
 18: 'alt',
 19: 'pause',
 20: 'caps_lock',
 27: 'esc',
 32: 'spacebar',
 33: 'page_up',
 34: 'page_down',
 35: 'end',
 36: 'home',
 37: 'left_arrow',
 38: 'up_arrow',
 39: 'right_arrow',
 40: 'down_arrow',
 41: 'select',
 42: 'print',
 43: 'execute',
 44: 'print_screen',
 45: 'ins',
 46: 'del',
 47: 'help',
 48: '0',
 49: '1',
 50: '2',
 51: '3',
 52: '4',
 53: '5',
 54: '6',
 55: '7',
 56: '8',
 57: '9',
 65: 'a',
 66: 'b',
 67: 'c',
 68: 'd',
 69: 'e',
 70: 'f',
 71: 'g',
 72: 'h',
 73: 'i',
 74: 'j',
 75: 'k',
 76: 'l',
 77: 'm',
 78: 'n',
 79: 'o',
 80: 'p',
 81: 'q',
 82: 'r',
 83: 's',
 84: 't',
 85: 'u',
 86: 'v',
 87: 'w',
 88: 'x',
 89: 'y',
 90: 'z',
 96: 'numpad_0',
 97: 'numpad_1',
 98: 'numpad_2',
 99: 'numpad_3',
 100: 'numpad_4',
 101: 'numpad_5',
 102: 'numpad_6',
 103: 'numpad_7',
 104: 'numpad_8',
 105: 'numpad_9',
 106: 'multiply_key',
 107: 'add_key',
 108: 'separator_key',
 109: 'subtract_key',
 110: 'decimal_key',
 111: 'divide_key',
 112: 'F1',
 113: 'F2',
 114: 'F3',
 115: 'F4',
 116: 'F5',
 117: 'F6',
 118: 'F7',
 119: 'F8',
 120: 'F9',
 121: 'F10',
 122: 'F11',
 123: 'F12',
 124: 'F13',
 125: 'F14',
 126: 'F15',
 127: 'F16',
 128: 'F17',
 129: 'F18',
 130: 'F19',
 131: 'F20',
 132: 'F21',
 133: 'F22',
 134: 'F23',
 135: 'F24',
 144: 'num_lock',
 145: 'scroll_lock',
 160: 'left_shift',
 161: 'right_shift ',
 162: 'left_control',
 163: 'right_control',
 164: 'left_menu',
 165: 'right_menu',
 166: 'browser_back',
 167: 'browser_forward',
 168: 'browser_refresh',
 169: 'browser_stop',
 170: 'browser_search',
 171: 'browser_favorites',
 172: 'browser_start_and_home',
 173: 'volume_mute',
 174: 'volume_Down',
 175: 'volume_up',
 176: 'next_track',
 177: 'previous_track',
 178: 'stop_media',
 179: 'play/pause_media',
 180: 'start_mail',
 181: 'select_media',
 182: 'start_application_1',
 183: 'start_application_2',
 186: ';',
 187: '+',
 188: ',',
 189: '-',
 190: '.',
 191: '/',
 192: '`',
 219: '[',
 220: '\\',
 221: ']',
 222: "'",
 246: 'attn_key',
 247: 'crsel_key',
 248: 'exsel_key',
 250: 'play_key',
 251: 'zoom_key',
 254: 'clear_key'}
另外,您应该对照
-127
检查
int(win32api.GetKeyState(key))
,如果切换键,它可以是
1