简体   繁体   中英

keyboard emulation with windows and python

I'm trying to achieve the same under Windows as I have achieved in Linux environment. Serial to keyboard input, someone please help or point me in the right direction. I'd should be as simple as possible, I tried but failed.

import serial
import time
import uinput
import binascii

ser = serial.Serial(
    port='/dev/ttyUSB0',\
    baudrate=115200,\
    parity=serial.PARITY_NONE,\
    stopbits=serial.STOPBITS_ONE,\
    bytesize=serial.EIGHTBITS,\
    timeout=0)

ser.open()

device = uinput.Device([uinput.KEY_A, uinput.KEY_B])

time.sleep(1)
while True:
    datain=ser.read(1)
    if datain=='':
        continue
    datain_int=int(binascii.hexlify(datain), 16)
    datain_bin=bin(datain_int)
    if datain_int==0:
        continue
    if datain_int==128:
        device.emit_click(uinput.KEY_A)
    elif datain_int==64:
        device.emit_click(uinput.KEY_B)

I'm not sure what you're trying to do exactly, but here's a script I once wrote to build a hotkey cheat engine for GTA San Andreas

import pyHook
import pythoncom
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
hm = pyHook.HookManager()
def OnKeyboardEvent(event):
    if event.KeyID == 48:
        #cheat set 1
        shell.SendKeys("chttychttybangbang")
    if event.KeyID == 49:
        #cheat set 2
        shell.SendKeys("hesoyamuzumymwfullclip")
    if event.KeyID == 50:
        #cheat set 3
        shell.SendKeys("hesoyaprofessionalskitfullclip")
    if event.KeyID == 51:
        #cheat set 4
        shell.SendKeys("hesoyalxgiwylfullclip")
    if event.KeyID == 52:
        #cheat set 5
        shell.SendKeys("zeiivgylteiczflyingfisheveryoneisrichspeedfreak")
    if event.KeyID == 53:
        #cheat set 6
        shell.SendKeys("aiwprton")
    if event.KeyID == 54:
        #cheat set 7
        shell.SendKeys("oldspeeddemon")
    if event.KeyID == 55:
        #cheat set 8
        shell.SendKeys("itsallbull")
    if event.KeyID == 56:
        #cheat set 9
        shell.SendKeys("monstermash")
    if event.KeyID == 57:
        #cheat set 10
        shell.SendKeys("jumpjetkgggdkpaiypwzqpohdudeflyingtostunt")
    # return True to pass the event to other handlers
    return True
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()

And it worked on windows. Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM