Search code examples
c++avrhidatmegacustom-keyboard

Why my HID keyboard doesn't work very well?


I've just started programming my AtMega8 with using V-USB link: https://www.obdev.at/products/vusb/index.html. I'm trying to make a keyboard witch will be pushing CTRL+ALT. To do this I modified one of the project "HID Keys" link: https://www.obdev.at/products/vusb/hidkeys.html. I added and modified:

//line 150, added definitions
#define KEY_DELETE 42
#define ALT_RIG    230 
#define ALT_LEF 226
#define CTRL_LEF    224

(I have values of definitions from this side: https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf (from page 53))

//line 204, modified code
static const uchar  keyReport[NUM_KEYS + 1][2] PROGMEM = {
/* none */  {0, 0},                     /* no key pressed */
/*  1 */    {0, KEY_A},
/*  2 */    {0, KEY_B},
/*  3 */    {0, KEY_C},
/*  4 */    {0, KEY_D},
/*  5 */    {0, KEY_E},
/*  6 */    {0, KEY_F},
/*  7 */    {0, KEY_G},
/*  8 */    {0, KEY_H},
/*  9 */    {0, KEY_I},
/* 10 */    {0, 0},
/* 11 */    {0, 0},
/* 12 */    {MOD_CONTROL_LEFT, ALT_RIG}, //CTRL+ALT
/* 13 */    {0, KEY_J},
/* 14 */    {0, KEY_K},
/* 15 */    {0, KEY_L},
/* 16 */    {0, KEY_M},
/* 17 */    {0, KEY_N},
};

Some kind of reason, "clicking" the keys (CTRL+ALT) by uC (AtMega8) does not working. (I check this by pushing at the same time (when keys from uC are (should be) pushed) the DEL key on my "true" keyboard - then the "characteristic" system management window (Windows 7) should appear.) I have no idea why this isn't work. How can I do this, that my uC will be pushing CTRL+ALT?


Solution

  • The report descriptor in the sample program defines a 2-byte report to represent a single keystroke. The first byte contains 8 bits each representing a key modifier (left ctrl, right alt, left shift etc) and the next byte is an index representing a key usage (key A, key B, key Delete, etc).

    You need to logically OR the desired key modifiers in the first byte. The easiest way is to simply add them together. For example, to send Ctrl+Alt+Delete you could code:

    {MOD_CONTROL_LEFT+MOD_ALT_LEFT, KEY_DELETE}
    

    Don't forget to send {0, 0} afterwards to indicate that no keys are currently pressed.