Pages: [1]   Go Down
Author Topic: keyboard.print vs. keyboard.write vs. keyboard.press?  (Read 655 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 158
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).

Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 481
Posts: 18742
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Code:
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:
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.
Logged


Offline Offline
Full Member
***
Karma: 0
Posts: 158
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks so much for your help, guys!
Logged

Pages: [1]   Go Up
Jump to: