Numpad keys

Hi there, I was wondering if was possible to program a Numlock Key as a keyboard press? Such as the Numlock Key 7? If so, how?
Thanks!

With the Arduino Leonardo ?
You can send that code and see what happens.

This is the reference for the keys: http://arduino.cc/en/Reference/KeyboardModifiers
This is the code definition: http://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/USBAPI.h

Do you mean Number-pad key 7?

Numlock is usually a toggle, it isn't meaningfull to say "Numlock Key 7".

Sorry, my bad for poor choice of words and details, am new at this.

Yes it is an Arduino Leonardo, and I don't see the Number-pad key 7 on there, not sure how to code that in.

Any help is appreciated!

I presume it is possible, the documentation is hard to find. Here for example:

http://www.quadibloc.com/comp/scan.htm

I am finding quite a few ways of sending (say) F1, none of which seem to send the number documented here: http://arduino.cc/en/Reference/KeyboardModifiers

Hmmm - after reading the code a bit more, it seems that if you do a Keyboard.press (n) where n >= 136, then it subtracts 136 and sends that as a hex code.

So for example:

 KEY_F1 	0xC2 	194

194 - 136 = 58

58 in hex is 0x3A

From that page (Scan Codes Demystified) 0x3A is F1.

So working forwards from that page:

Keypad-7 is 0x5F.

Add 136:

 0x5f + 136 = 231

So, try doing:

Keyboard.press (231);

Just guessing, but maybe that will work. :slight_smile:

(edit) Don't forget to do a Keyboard.release afterwards.

Amazing, it worked!
I am still not sure how you got the hex number for Keypad 7 to be 0x5F, I am looking at a few links including the one you posted and nothing makes sense to me.
Anyways, would you mind telling me what Keypad 4 and 5 are please?
Thank you very much for your help!

Well done Nick, an undocumented feature !

KEYPAD_7 = 231
The others are in the same range.

KEYPAD_6 = 230
KEYPAD_5 = 229
KEYPAD_4 = 228
and so on

Here's the table:

Scan Code               Key       Scan Code               Key         Scan Code               Key
  Set    Set  Set  USB              Set    Set  Set  USB                Set    Set  Set  USB
   1      2    3                     1      2    3                       1      2    3

   01     76   08   29  Esc          37     7C            * PrtSc     E0 5E  E0 37            Power
   02     16   16   1E  ! 1          37+    7C+  7E   55  * KP        E0 5F  E0 3F            Sleep
   03     1E   1E   1F  @ 2       37/54+ 7C/84   57   46  PrtSc       E0 63  E0 5E            Wake
   04     26   26   20  # 3          38     11   19   E2  Alt L       E0 20  E0 23        7F  Mute
   05     25   25   21  $ 4       E0 38  E0 11   39   E6  Alt R       E0 30  E0 33        80  Volume Up
   06     2E   2E   22  % 5          39     29   29   2C  Space       E0 2E  E0 21        81  Volume Down
   07     36   36   23  ^ 6          3A     58   14   39  Caps Lock   E0 17  E0 43        7B  Cut
   08     3D   3D   24  & 7          3B     05   07   3A  F1          E0 18  E0 44        7C  Copy
   09     3E   3E   25  * 8          3C     06   0F   3B  F2          E0 0A  E0 46        7D  Paste
   0A     46   46   26  ( 9          3D     04   17   3C  F3          E0 3B  E0 05        75  Help
   0B     45   45   27  ) 0          3E     0C   1F   3D  F4          E0 08  E0 3D        7A  Undo
   0C     4E   4E   2D  _ -          3F     03   27   3E  F5          E0 07  E0 36            Redo
   0D     55   55   2E  + =          40     0B   2F   3F  F6          E0 22  E0 34            Play
   0E     66   66   2A  Back Space   41     83   37   40  F7          E0 24  E0 3B            Stop
   0F     0D   0D   2B  Tab          42     0A   3F   41  F8          E0 10  E0 15            Skip Back
   10     15   15   14  Q            43     01   47   42  F9          E0 19  E0 4D            Skip Fwd
   11     1D   1D   1A  W            44     09   4F   43  F10         E0 2C  E0 1A            Eject
   12     24   24   08  E            45+    77+  76   53  Num Lock    E0 1E  E0 1C            Mail
   13     2D   2D   15  R         45/46+ 77/7E+  62   48  Pause/Bk    E0 32  E0 3A            Web
   14     2C   2C   17  T            46     7E            ScrLk/Bk    E0 3C  E0 06            Music
   15     35   35   1C  Y            46+    7E+  5F   47  Scroll Lock E0 64  E0 08            Pictures
   16     3C   3C   18  U            47     6C   6C   5F  7 Home KP   E0 6D  E0 50            Video
   17     43   43   0C  I         E0 47* E0 6C*  6E   4A  Home CP
   18     44   44   12  O            48     75   75   60  8 Up KP        5B     1F   08   68  F13
   19     4D   4D   13  P         E0 48* E0 75*  63   52  Up CP          5C     27   10   69  F14
   1A     54   54   2F  { [          49     7D   7D   61  9 PgUp KP      5D     2F   18   6A  F15
   1B     5B   5B   30  } ]       E0 49* E0 7D*  6F   4B  PgUp CP        63     5E   2C   6B  F16
   1C     5A   5A   28  Enter        4A     7B   84   56  - KP           64     08   2B   6C  F17
E0 1C  E0 5A   79   58  Enter KP     4B     6B   6B   5C  4 Left KP      65     10   30   6D  F18
   1D     14   11   E0  Ctrl L    E0 4B* E0 6B*  61   50  Left CP        66     18   38   6E  F19
E0 1D  E0 14   58   E4  Ctrl R       4C     73   73   97  5 KP           67     20   40   6F  F20
   1E     1C   1C   04  A            4D     74   74   5E  6 Right KP     68     28   48   70  F21     
   1F     1B   1B   16  S         E0 4D* E0 74*  6A   4F  Right CP       69     30   50   71  F22
   20     23   23   07  D            4E     79   7C   57  + KP           6A     38   57   72  F23
   21     2B   2B   09  F            4F     69   69   59  1 End KP       6B     40   5F   73  F24
   22     34   34   0A  G         E0 4F* E0 69*  65   4D  End CP                          75              Help
   23     33   33   0B  H            50     72   72   5A  2 Down KP     [71]    19   05   9A  Attn  SysRq
   24     3B   3B   0D  J         E0 50* E0 72*  60   51  Down CP        76     5F   06   9C  Clear
   25     42   42   0E  K            51     7A   7A   5B  3 PgDn KP                       76              Stop
   26     4B   4B   0F  L         E0 51* E0 7A*  6D   4E  PgDn CP                         77              Again
   27     4C   4C   33  : ;          52     70   70   62  0 Ins KP       72     39   04   A3  CrSel       Properties
   28     52   52   34  " '       E0 52* E0 70*  67   49  Ins CP                     0C       Pause ErInp
   29     0E   0E   35  ~ `          53     71   71   63  . Del KP                        78              Undo
   2A     12   12   E1  Shift L   E0 53* E0 71*  64   4C  Del CP         74     53   03   A4  ExSel SetUp
   2B     5D   5C   31  | \          54     84            SysRq          6D     50   0E       ErEOF Recrd
   2B     5D   53   53  (INT 2)      56     61   13   64  (INT 1)        
   2C     1A   1A   1D  Z            57     78   56   44  F11                             80              Copy
   2D     22   22   1B  X            58     07   5E   45  F12                        83       Print Ident
   2E     21   21   06  C         E0 5B  E0 1F   8B   E3  Win L          6F     6F   0A       Copy  Test
   2F     2A   2A   19  V         E0 5C  E0 27   8C   E7  Win R          
   30     32   32   05  B         E0 5D  E0 2F   8D   65  WinMenu                         81              Paste
   31     31   31   11  N            70     13   87   88  katakana       75     5C   01       Enl   Help
   32     3A   3A   10  M            73     51   51   87  (INT 3)        6C     48   09       Ctrl
   33     41   41   36  < ,          77     62        8C  furigana                        82              Find
   34     49   49   37  > .          79     64   86   8A  kanji                           79              Cut
   35     4A   4A   38  ? /          7B     67   85   8B  hiragana
   35+    4A+  77   54  / KP         7D     6A   5D   89  (INT 4)     E0 4C  E0 73   62       Rule
   36     59   59   E5  Shift R     [7E]    6D   7B       (INT 5)

This part is relevant:

47 6C 6C 5F 7 Home KP

That is under the USB column.

So for 4 and 5:

4B 6B 6B 5C 4 Left KP
4C 73 73 97 5 KP

Adding 136 I get:

 0x5c + 136 = 228 // keypad 4
 0x97 + 136 = 287 // keypad 5

Now I forsee a problem here, because 287 exceeds what will fit into a byte. I can't see any easy way around this, maybe someone else can. Changing "Keyboard_::press" in HID.cpp to take an unsigned int rather than a byte might be a simple way.

Maybe change:

size_t Keyboard_::press(uint8_t k)

to:

size_t Keyboard_::press(unsigned int k)

I'm not sure if that will work or not.


KEYPAD_6 = 230
KEYPAD_5 = 229
KEYPAD_4 = 228

Did I misread the diagram?

The diagram might be wrong. Looking at another source it seems likely the codes are (in decimal):

KEYPAD_SLASH     84  
KEYPAD_ASTERIX   85  
KEYPAD_MINUS     86  
KEYPAD_PLUS      87  
KEYPAD_ENTER     88  
KEYPAD_1         89  
KEYPAD_2         90  
KEYPAD_3         91  
KEYPAD_4         92  
KEYPAD_5         93  
KEYPAD_6         94  
KEYPAD_7         95  
KEYPAD_8         96  
KEYPAD_9         97  
KEYPAD_0         98  
KEYPAD_PERIOD    99

So just add 136 to each of those.

So with the addition done:

const byte KEYPAD_0 = 234;
const byte KEYPAD_1 = 225;
const byte KEYPAD_2 = 226;
const byte KEYPAD_3 = 227;
const byte KEYPAD_4 = 228;
const byte KEYPAD_5 = 229;
const byte KEYPAD_6 = 230;
const byte KEYPAD_7 = 231;
const byte KEYPAD_8 = 232;
const byte KEYPAD_9 = 233;
const byte KEYPAD_ASTERIX = 221;
const byte KEYPAD_ENTER = 224;
const byte KEYPAD_MINUS = 222;
const byte KEYPAD_PERIOD = 235;
const byte KEYPAD_PLUS = 223;
const byte KEYPAD_SLASH = 220;

These are suitable for use with Keyboard.press, eg.

Keyboard.press (KEYPAD_PERIOD);

And since I am on a bit of a roll here with this, I converted all the symbols from the other file I had:

const byte KEYPAD_0 = 234;
const byte KEYPAD_1 = 225;
const byte KEYPAD_2 = 226;
const byte KEYPAD_3 = 227;
const byte KEYPAD_4 = 228;
const byte KEYPAD_5 = 229;
const byte KEYPAD_6 = 230;
const byte KEYPAD_7 = 231;
const byte KEYPAD_8 = 232;
const byte KEYPAD_9 = 233;
const byte KEYPAD_ASTERIX = 221;
const byte KEYPAD_ENTER = 224;
const byte KEYPAD_MINUS = 222;
const byte KEYPAD_PERIOD = 235;
const byte KEYPAD_PLUS = 223;
const byte KEYPAD_SLASH = 220;
const byte KEY_BACKSLASH = 185;
const byte KEY_BACKSPACE = 178;
const byte KEY_CAPS_LOCK = 193;
const byte KEY_COMMA = 190;
const byte KEY_DELETE = 212;
const byte KEY_DOWN = 217;
const byte KEY_END = 213;
const byte KEY_ENTER = 176;
const byte KEY_EQUAL = 182;
const byte KEY_ESC = 177;

const byte KEY_F1 = 194;
const byte KEY_F2 = 195;
const byte KEY_F3 = 196;
const byte KEY_F4 = 197;
const byte KEY_F5 = 198;
const byte KEY_F6 = 199;
const byte KEY_F7 = 200;
const byte KEY_F8 = 201;
const byte KEY_F9 = 202;
const byte KEY_F10 = 203;
const byte KEY_F11 = 204;
const byte KEY_F12 = 205;
const byte KEY_F13 = 240;
const byte KEY_F14 = 241;
const byte KEY_F15 = 242;
const byte KEY_F16 = 243;
const byte KEY_F17 = 244;
const byte KEY_F18 = 245;
const byte KEY_F19 = 246;
const byte KEY_F20 = 247;
const byte KEY_F21 = 248;
const byte KEY_F22 = 249;
const byte KEY_F23 = 250;
const byte KEY_F24 = 251;

const byte KEY_HOME = 210;
const byte KEY_INSERT = 209;
const byte KEY_LEFT = 216;
const byte KEY_LEFT_BRACE = 183;
const byte KEY_MENU = 237;
const byte KEY_MINUS = 181;
const byte KEY_NON_US_NUM = 186;
const byte KEY_NUM_LOCK = 219;
const byte KEY_PAGE_DOWN = 214;
const byte KEY_PAGE_UP = 211;
const byte KEY_PAUSE = 208;
const byte KEY_PERIOD = 191;
const byte KEY_PRINTSCREEN = 206;
const byte KEY_QUOTE = 188;
const byte KEY_RIGHT = 215;
const byte KEY_RIGHT_BRACE = 184;
const byte KEY_SCROLL_LOCK = 207;
const byte KEY_SEMICOLON = 187;
const byte KEY_SLASH = 192;
const byte KEY_SPACE = 180;
const byte KEY_TAB = 179;
const byte KEY_TILDE = 189;
const byte KEY_UP = 218;

You will probably get compile errors with the above because of clashing #define clauses. This might work better:

#define KEYPAD_0 234 
#define KEYPAD_1 225 
#define KEYPAD_2 226 
#define KEYPAD_3 227 
#define KEYPAD_4 228 
#define KEYPAD_5 229 
#define KEYPAD_6 230 
#define KEYPAD_7 231 
#define KEYPAD_8 232 
#define KEYPAD_9 233 
#define KEYPAD_ASTERIX 221 
#define KEYPAD_ENTER 224 
#define KEYPAD_MINUS 222 
#define KEYPAD_PERIOD 235 
#define KEYPAD_PLUS 223 
#define KEYPAD_SLASH 220 
#define KEY_BACKSLASH 185 
#define KEY_BACKSPACE 178 
#define KEY_CAPS_LOCK 193 
#define KEY_COMMA 190 
#define KEY_DELETE 212 
#define KEY_DOWN 217 
#define KEY_END 213 
#define KEY_ENTER 176 
#define KEY_EQUAL 182 
#define KEY_ESC 177 

#define KEY_F1 194 
#define KEY_F2 195 
#define KEY_F3 196 
#define KEY_F4 197 
#define KEY_F5 198 
#define KEY_F6 199 
#define KEY_F7 200 
#define KEY_F8 201 
#define KEY_F9 202 
#define KEY_F10 203 
#define KEY_F11 204 
#define KEY_F12 205 
#define KEY_F13 240 
#define KEY_F14 241 
#define KEY_F15 242 
#define KEY_F16 243 
#define KEY_F17 244 
#define KEY_F18 245 
#define KEY_F19 246 
#define KEY_F20 247 
#define KEY_F21 248 
#define KEY_F22 249 
#define KEY_F23 250 
#define KEY_F24 251 

#define KEY_HOME 210 
#define KEY_INSERT 209 
#define KEY_LEFT 216 
#define KEY_LEFT_BRACE 183 
#define KEY_MENU 237 
#define KEY_MINUS 181 
#define KEY_NON_US_NUM 186 
#define KEY_NUM_LOCK 219 
#define KEY_PAGE_DOWN 214 
#define KEY_PAGE_UP 211 
#define KEY_PAUSE 208 
#define KEY_PERIOD 191 
#define KEY_PRINTSCREEN 206 
#define KEY_QUOTE 188 
#define KEY_RIGHT 215 
#define KEY_RIGHT_BRACE 184 
#define KEY_SCROLL_LOCK 207 
#define KEY_SEMICOLON 187 
#define KEY_SLASH 192 
#define KEY_SPACE 180 
#define KEY_TAB 179 
#define KEY_TILDE 189 
#define KEY_UP 218

I have tested it for KEYPAD_0 to KEYPAD_9 from an Arduino Leonardo, in linux with xev, and compared to numpad with NumLock on. They result into the same code. So that is very nice.

The 'xev' also shows the scan code, which is not incremental for 0...9. I guess the code used by an USB keyboard (and the Arduino Leonardo) is one level higher than the scan code.

I was wondering what the unused numbers will do.
The new defines are now 176 ... 251, without the numbers : 238, 239.
However, 238, 239, and also 252, 253, 254, 255 do nothing at all in xev.
The modifiers from this page: http://arduino.cc/en/Reference/KeyboardModifiers also contain 128...135.
So I also tested 136 to 175. The value 140 ... 175 are mapped to a ... z, 1 ... 9, 0. The rest is ignored.
I don't know why they are mapped, code 140 is exactly the same as pressing key 'a' on my keyboard. Perhaps this is only in linux.

My computer accepts KEY_F12, but not KEY_F13 to KEY_F24, those are also ignored.

The 'Windows' key is called KEY_LEFT_GUI, I have to get used to that.
I can create an euro sign with KEY_RIGHT_ALT and '5' (my keyboard is set to for that). That's nice. Even the Scroll Lock works. But sending NumLock, ScrollLock and CapsLock keys do not set the leds on the keyboard.

KEY_A is 4, so that would be 140 once you add 136 (and so on).

I didn't bother doing a-z because you get that anyway with Keyboard.print.

My computer accepts KEY_F12, but not KEY_F13 to KEY_F24, those are also ignored.

Might be the type of keyboard you tell the OS you have installed.