Go Down

Topic: keyboard.print vs. keyboard.write vs. keyboard.press? (Read 2090 times) previous topic - next topic


I was reading up on keystrokes here: http://arduino.cc/en/Reference/MouseKeyboard

I'm still not entirely sure what the differences are between those three functions. Could somebody please explain the differences to me a bit better? I'm writing a program that involves sending keystrokes to my computer, and I'm not quite sure which one is the appropriate one to use. Thanks.


The Keyboard.press() should be followed by a Keyboard.release(). That is how your 'real' keyboard is. You could keep the button pressed a long time. For example, if you want to move forward in a game, you want to keep pressing that button.

To make it simpler for you, the Keyboard.write() sends a single character. Like you press a button on the keyboard and release it.

To send a string of text, the Keyboard.print() function is used. Like typing a whole sentence.

To send a string of text followed by Enter, the function Keyboard.println() is used.

Why so many functions ? Well, the 'normal' basic functions are the Keyboard.press() and the Keyboard.release(). The other functions are a result of the 'stream' class. The 'stream' class is a common class used by other classes, it uses the same functions for all sorts of communication (write,print,println).


A write (or print) is basically a press + release.

So to send "normal" text, writing or printing is fine. eg.

Code: [Select]

Keyboard.print ("hi there");

However if you want to press a key (eg. Shift) and then press another key (eg. Enter) then you would probably do 2 x press and then a releaseAll.

This is the code for write:

Code: [Select]

size_t Keyboard_::write(uint8_t c)
uint8_t p = press(c); // Keydown
uint8_t r = release(c); // Keyup
return (p); // just return the result of press() since release() almost always returns 1

So you can see it just presses and releases the required key.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics


Go Up