2013-08-07 39 views
0

我試圖在Windows環境下實現同樣的功能。串口到鍵盤輸入,請有人幫忙或指點我正確的方向。我應該儘可能簡單,我試過但失敗了。用windows和python進行鍵盤模擬

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) 

回答

1

我不知道你想要做什麼,但這裏有一個腳本,我曾經寫過建立一個熱鍵作弊引擎GTA聖安地列斯

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() 

和它的工作在Windows 。希望這可以幫助

+0

嘿謝謝,即時嘗試使用任天堂64控制器,我會嘗試這一點,然後接受你的答案。謝謝 –