For some odd reason, Visual Boy Advance seems to neither respond to SendMessage nor keybd_event when used on their own. But if you use them together, it starts working. So using WinAPI in C++ (you can translate into Python if needed; I'm not familiar with Python enough), this sendKeys(window,key) method works with Visual Boy Advance:
/* Just a struct to make keypress messages more organized and easier to set up */
struct extraKeyInfo {
unsigned short repeatCount;
unsigned char scanCode;
bool extendedKey, prevKeyState, transitionState;
/* Convert this struct's data into the properly-encoded unsigned int on casting */
operator unsigned int()
{
return repeatCount | (scanCode << 16) | (extendedKey << 24) |
(prevKeyState << 30) | (transitionState << 31);
}
};
/* The main method */
void sendKey(HWND hCurrentWindow, BYTE keyval) {
extraKeyInfo lParam = {};
BYTE vkCode=keyval;
lParam.scanCode = MapVirtualKey(vkCode, 0);
keybd_event(vkCode, lParam.scanCode, 0x0, 0x0);
SendMessage(hCurrentWindow, WM_KEYDOWN, vkCode, lParam);
lParam.repeatCount = 1;
lParam.prevKeyState = true;
lParam.transitionState = true;
keybd_event(vkCode, lParam.scanCode, 0x2, 0x0);
SendMessage(hCurrentWindow, WM_KEYUP, vkCode, lParam);
}
Note that I've only tested this with VBA in the foreground, so if it's in the background it may not work, but it works 100% of the time in my foreground tests.