tags:

views:

26

answers:

1

I've made an interface to a NES Controller with an atxmega, which send the keys through serial. The problem lies in the program that reads the input and sends keybd_events accordingly.

if(szBuff[0] & BTN_LEFT) {
    keybd_event(VkKeyScan('j'), 0, 0, 0);
    keybd_event(VkKeyScan('j'), 0, KEYEVENTF_KEYUP, 0);
}

Problem is, it fails to get recognized as a 'j' in a NES emulator, while it does so in say a browser. Any ideas?

A: 

Hard to answer this without you explaining how the Nintendo emulator works. But you'll have to provide the virtual key code to VkKeyScan(). Which is 'J', not 'j'. As long as the Shift key isn't pressed, Windows will translate that to a WM_CHAR message that generates a 'j'.

Note that keybd_event() takes a virtual key and a scan code. You are passing the scan code as the virtual key. Fix:

 keybd_event('J', VkKeyScan('J'), 0, 0);
Hans Passant
I'm sorry, the emulator is FCEUX, but I've been unsuccessful in figuring out how it reads the keyboard state. One thing I have noticed, in the button configuration, it notices when an event has been sent (button has been pressed), but it assumes the keycode sent is 0x00. Switching to 'J' did not work. Does keybd_event maybe send more than just the keypress? I'm not that familiar with WINAPI.
John
Fantastic, it works perfectly now. Thanks for the help!
John