If you have Arduino/Genuino Uno selected as your board in the Tools ► Board ► menu, then you get the error. If you select Arduino Leonardo it compiles correctly.
From Can't seem to be able use Keyboard.h library, this post states:
Keyboard.h (and Mouse.h) only works on devices that use the ATmega32u4 processor, like the Leonardo or Micro. See: https://www.arduino.cc/en/Reference/MouseKeyboard
This solution might help though
I had the same problem using Flora
This worked for me:
- Tools > Boards > Boards Manager
- Install TeeOnArdu (Then select that board from Boards Manager)
- Tools > USB Type > Serial, Keyboard, Mouse < Joypad
- Upload and run code again (might need to restart IDE)