sunshinekitty sunshinekitty - 6 months ago 77
Python Question

Python 3.3 Send Keys to Visual Boy Advance

Following along with (http://win32com.goermezer.de/content/view/136/254/) I was able to load up a program, gain focus on the program, however I'm not able to send actual keys into the emulation, it's like it's sending to the window and not inside the emulation.

The bit of code that I am using goes like:

import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.Run("Silver.gbc")
shell.AppActivate("VisualBoyAdvance")
shell.SendKeys("{DOWN}")


Which all works until I try and send {DOWN}, I have also tried "z" and it won't send inside of the window, even though it sends fine to any other application. Any ideas? Thanks in advance.

Answer

Problem was that SendKey isn't compatible with Direct Input, to get past this I used win32api's keybd_event for direct input

VK_CODE = {
    'backspace':0x08
}

def press(*args):
    '''
    press, release
    eg press('x', 'y', 'z')
    '''
    for i in args:
        win32api.keybd_event(VK_CODE[i], 0, 0, 0)
        time.sleep(0.2)
        win32api.keybd_event(VK_CODE[i],0 ,win32con.KEYEVENTF_KEYUP ,0)

press('backspace')