# Keyboard.press and .write do not work

**URL:** <https://community.particle.io/t/keyboard-press-and-write-do-not-work/28117>\
**Category:** Troubleshooting\
**Created:** [December 23, 2016, 9:11pm UTC](https://community.particle.io/t/keyboard-press-and-write-do-not-work/28117 "2016-12-23T21:11:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MatthewJGilbert](https://avatars.discourse-cdn.com/v4/letter/m/97f17d/32.png) [@MatthewJGilbert](https://community.particle.io/u/MatthewJGilbert)\
**Post date:** [December 23, 2016, 9:11pm UTC](https://community.particle.io/t/keyboard-press-and-write-do-not-work/28117/1 "2016-12-23T21:11:57Z")

</div>

I know that there is a lot of forum discussion regarding using a Photon as a Human Interface Device. I have read all of the previous threads regarding HID. We have a simple script which I will include below. **The Keyboard.print() function works well, however, Keyboard.press() and Keyboard.write() do nothing at all.** I have tried key codes i found on these forums, as well as the codes on [https://www.arduino.cc/en/Reference/KeyboardModifiers](https://www.arduino.cc/en/Reference/KeyboardModifiers) . Even Keyboard.press(‘n’) does not work. Thanks for the help, here is the code:

```
void setup() {
    Keyboard.begin();
}

void loop() {
    delay(1000);
    Keyboard.write(131); //Should press the left windows key, but does nothing
    delay(1000);
    Keyboard.print("www.deagware.xyz"); //print works
    delay(100000);
}

```

It should be noted I am using the 0.6.0-rc.2 firmware on a Photon

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [December 23, 2016, 9:43pm UTC](https://community.particle.io/t/keyboard-press-and-write-do-not-work/28117/2 "2016-12-23T21:43:43Z")

</div>

Have you seen this?  
[https://docs.particle.io/reference/firmware/photon/#write--1](https://docs.particle.io/reference/firmware/photon/#write--1)

> [@](#):
>
> This function works only with ASCII characters. ASCII characters are translated into USB HID keycodes according to the [conversion table](https://github.com/spark/firmware/blob/develop/wiring/src/spark_wiring_usbkeyboard.cpp#L33).

That might explain why _your_ code doesn't work, which doesn't mean the function doesn't do what it's supposed to do.

And for `press()` you might want to try a `release()` too - or just the `click()` which bundles both.  
And as for your `Keyboard.press('n')` what did you expect to happen, and what do the [docs](https://docs.particle.io/reference/firmware/photon/#click--1) say should happen?

`'n'` equals `0x6E` which in turn [maps to](https://github.com/spark/firmware/blob/develop/wiring/inc/spark_wiring_usbkeyboard_scancode.h#L115) `KEY_F19 = 0x6E, // Keyboard F19`

Did you check if `KEY_F19` was pressed or did you expect to get a load of letters `n` typed?

---

<div class="post-metadata">

**Author:** ![harrisonhjones](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/harrisonhjones/32/4132_2.png) [@harrisonhjones](https://community.particle.io/u/harrisonhjones)\
**Post date:** [December 25, 2016, 3:54pm UTC](https://community.particle.io/t/keyboard-press-and-write-do-not-work/28117/3 "2016-12-25T15:54:06Z")

</div>

Did you make any progress on this? It looks like @ScruffR explained why `write` didn’t work.

It looks like `click()` should work however the Windows key isn’t called out in the [keycode enum map](https://github.com/spark/firmware/blob/develop/wiring/inc/spark_wiring_usbkeyboard_scancode.h). From [this SO post](http://stackoverflow.com/questions/36458625/keycode-for-windows-key) it looks like it could be `5B` or `5C` which map to `KEY_KP_3` and `KEY_KP_4` respectively in the keycode enum map.

**EDIT: Looks like it’s actually `KEY_LEFTGUI` and `KEY_RIGHTGUI` as @avtolstoy mentions below**

**EDIT 2: Added a [PR](https://github.com/spark/firmware/pull/1217) to address this issue**

---

<div class="post-metadata">

**Author:** ![avtolstoy](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/avtolstoy/32/8126_2.png) [@avtolstoy](https://community.particle.io/u/avtolstoy)\
**Post date:** [December 25, 2016, 4:57pm UTC](https://community.particle.io/t/keyboard-press-and-write-do-not-work/28117/4 "2016-12-25T16:57:20Z")

</div>

‘Windows’ key is actually `KEY_LEFTGUI` or `KEY_RIGHTGUI`. It’s also Command key on OSX and Meta key on \*nix.

---

<div class="post-metadata">

**Author:** ![UMD](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/umd/32/5850_2.png) [@UMD](https://community.particle.io/u/UMD)\
**Post date:** [January 1, 2017, 10:49am UTC](https://community.particle.io/t/keyboard-press-and-write-do-not-work/28117/5 "2017-01-01T10:49:10Z")

</div>

@MatthewJGilbert, I can positively confirm that Keyboard.press() and .write() both work okay.

@ScruffR has given excellent advice.

Perhaps refer to this ticket which showed my initial misunderstanding of what .press() is expecting:

> [@\[SOLVED\] \[0.6.0.rc2\] Keyboard.press() - how to?](https://community.particle.io/t/solved-0-6-0-rc2-keyboard-press-how-to/25704):
>
> @avtolstoy, seeing we are on a run with my testing… can you quickly assist here: I want to sent a Control A via the USB keyboard interface. I would have thought that the following should work, but it doesn’t: #include "spark\_wiring\_usbkeyboard\_scancode.h" … delay(100L); Keyboard.press(KEY\_LEFTCTRL); delay(100L); Keyboard.press(‘a’); // MUST be lower case Keyboard.releaseAll(); Thanks! Harry
