看本文章之前默认已经知道了winio的实现原理,这边文章主要讲winio在python中的运用。
按下“A”键,使用winio的时,伪代码:? ?SetByte($64,$D2); SetByte($60,$1E);
python的代码实现:
# 键盘和 winio中的编码的对应关系
vk = {'A': 0x1E,'B': 0x30, 'S': 0x1F,'W': 0x11,'DELETE':0x53}
KBC_KEY_CMD = 0x64
KBC_KEY_DATA = 0x60
# 按下按键代码
def key_down(scancode):
keyCode = vk[scancode]
winio = get_winio()
wait_for_buffer_empty();
winio.set_port_byte(KBC_KEY_CMD, 0xd2);
wait_for_buffer_empty();
winio.set_port_byte(KBC_KEY_DATA, keyCode)
# 松开按键代码
def key_up(scancode):
keyCode = vk[scancode]
winio = get_winio()
wait_for_buffer_empty()
winio.set_port_byte( KBC_KEY_CMD, 0xd2)
wait_for_buffer_empty()
winio.set_port_byte( KBC_KEY_DATA, keyCode | 0x80)
# 按下按键再松开
def key_press(scancode, press_time = 0.2):
key_down( scancode )
time.sleep( press_time )
key_up( scancode )
if __name__ == '__main__':
key_press('A')
如果是按下 DELETE 按键, 公共方法还重新写过。
DELETE E0,53 E0,D3
直接上代码:
def key_downE0(scancode):
keyCode = vk[scancode]
winio = get_winio()
wait_for_buffer_empty();
winio.set_port_byte(KBC_KEY_CMD, 0xd2);
wait_for_buffer_empty();
winio.set_port_byte(KBC_KEY_DATA, 0xE0)
wait_for_buffer_empty();
winio.set_port_byte(KBC_KEY_CMD, 0xd2);
wait_for_buffer_empty();
winio.set_port_byte(KBC_KEY_DATA, keyCode)
def key_upE0(scancode):
keyCode = vk[scancode]
winio = get_winio()
wait_for_buffer_empty();
winio.set_port_byte( KBC_KEY_CMD, 0xd2);
wait_for_buffer_empty();
winio.set_port_byte(KBC_KEY_DATA, 0xE0)
wait_for_buffer_empty();
winio.set_port_byte(KBC_KEY_CMD, 0xd2);
wait_for_buffer_empty();
winio.set_port_byte( KBC_KEY_DATA, keyCode | 0x80);
def key_pressE0(scancode, press_time = 0.2):
key_downE0( scancode )
time.sleep( press_time )
key_upE0( scancode )
if __name__ == '__main__':
key_press('A')
key_pressE0('DELETE')
感谢大家看我的文章有问题随时沟通
|