如何获取密钥的虚拟密钥代码 [英] How to get Virtual-Key-Code of a key

查看:189
本文介绍了如何获取密钥的虚拟密钥代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个将接收ascii密钥并将其转换为虚拟密钥代码的函数.

I am trying to write a function that will receive an ascii key and will convert it to a Virtual-key-code.

例如:

from msvcrt import getch
key= ord(getch()) #getting the ASCII character for a key that was pressed
def(key):
   #converting the key to virtual key code

例如: a的ascii代码是41.我希望函数接收它并返回0x41,这是密钥的虚拟密钥代码.

for example: the ascii code of a is 41. I want the function to receive it and return 0x41-which is the virtual key code of the key.

在此先感谢您的帮助!

推荐答案

您可以从Windows API使用VkKeyScan:

You can use VkKeyScan from Windows API:

from ctypes import windll

def char2key(c):
    # https://msdn.microsoft.com/en-us/library/windows/desktop/ms646329(v=vs.85).aspx
    result = windll.User32.VkKeyScanW(ord(unicode(c)))
    shift_state = (result & 0xFF00) >> 8
    vk_key = result & 0xFF

    return vk_key

这篇关于如何获取密钥的虚拟密钥代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆