| 1 |
// Listing 10.1 Standard code to create a Hid event |
| 2 |
// Declare a new Hid object |
| 3 |
Hid myHid; // (1) Makes a new Hid. |
| 4 |
|
| 5 |
// message to convey data from Hid device |
| 6 |
HidMsg msg; // (2) Makes a Hid message holder. |
| 7 |
|
| 8 |
// device number: which keyboard to open |
| 9 |
0 => int device; // (3) Opens Hid on keyboard device. |
| 10 |
|
| 11 |
// open keyboard; or exit if fail to open |
| 12 |
if( !myHid.openKeyboard( device ) ) |
| 13 |
{ // (4) Error if it can't be opened. |
| 14 |
<<< "Can't open this device!! ", "Sorry." >>>; |
| 15 |
me.exit(); // (5) Exit, because nothing more can be done. |
| 16 |
} |
| 17 |
|
| 18 |
// print status of open keyboard Hid |
| 19 |
// (6) If success, print happy message. |
| 20 |
<<< "keyboard '" + myHid.name() + "' ready", "" >>>; |
| 21 |
|
| 22 |
// Testing the keyboard Hid |
| 23 |
// Impulse keyboard "clicker" |
| 24 |
Impulse imp => dac; // (7) "Clicker" to hear key strokes. |
| 25 |
|
| 26 |
// infinite event loop |
| 27 |
while( true ) // (8) Infinite loop. |
| 28 |
{ |
| 29 |
// wait for event |
| 30 |
myHid => now; // (9) Wait here for a Hid event. |
| 31 |
|
| 32 |
// get message(s) |
| 33 |
while( myHid.recv( msg ) ) |
| 34 |
{ // (10) Loop over all messages. |
| 35 |
// Process only key (button) down events |
| 36 |
if( msg.isButtonDown() ) |
| 37 |
{ // (11) If keydown... |
| 38 |
// print ascii value and make a click |
| 39 |
<<< "key DOWN:", msg.ascii >>>; // (12) ...then print which key... |
| 40 |
5 => imp.next; // (13) ...and make click. |
| 41 |
} |
| 42 |
else // key (button) up |
| 43 |
{ |
| 44 |
// do nothing for now // (14) Do nothing on keyUp |
| 45 |
// (you could add something here). |
| 46 |
} |
| 47 |
} |
| 48 |
} |