Ascii arrow keys for Teensy???

robedney

Member
Hi,

I've started using a Teensy 3.2 instead of a Leonardo for my project. All is working well, except for the fact that the Teensy won't send arrow keys (up,down,left,right) with the same Ascii code as the Leonardo. I'm using the Arduino Keyboard.h library. For example, the left arrow key program line "if (counts >257&& counts <271) Keyboard.write (216);" works with the Leonardo, but outputs nothing to the Teensy. What code do I need for arrow keys???? Any help much appreciated for this semi-newbe!
 
As a quick test, I tried running this on a Teensy 3.2 with a couple pushbuttons connected.

Code:
#include <Bounce.h>

Bounce button1 = Bounce(1, 5);  // 5 = 5 ms debounce time
Bounce button2 = Bounce(2, 5);

void setup() {
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
}

void loop() {
  button1.update();
  button2.update();

  if (button1.fallingEdge()) {
    Keyboard.press(KEY_LEFT);
  }
  if (button2.fallingEdge()) {
    Keyboard.press(KEY_RIGHT);
  }
  
  if (button1.risingEdge()) {
    Keyboard.release(KEY_LEFT);
  }
  if (button2.risingEdge()) {
    Keyboard.release(KEY_RIGHT);
  }
}

It seems to work fine. If fact it's still running while I type this message, and if I reach over and tap the 2 buttons, my cursor moves back and forth.

Tested with Teensy 3.2, all settings at defaults, except of course Tools > USB Type set to Keyboard. Here's the hardware on my workbench used for this quick test.

1.jpg
 
I see you've started a duplicate thread. That's not the way this forum works.

We really do help here, but we expect you to show us the problem. You may have noticed "Forum Rule" in red at the top of every page. This is for a good reason. If you show us the code which isn't working, we can usually help much better. When we can't see what you're doing, help is limited to blind guesswork or simple advice (as msg #2) or a simple test (msg #4) which isn't nearly as good as being able to see the problem and actually help with a solution.

Please, show the problem. That is the way to get help here. Don't just create duplicate threads which again ask for help but don't show what you're actually doing that isn't working. The reason you get no replies, or replies like I have tried here which didn't help, is because you didn't show enough of what you're doing.
 
@robedney @Paul:

As mentioned, hard to help with little information...

However: from first post:
Code:
if (counts >257&& counts <271) Keyboard.write (216);
Which by the way from the Arduino Keyboard.h the 216 is
Code:
#define KEY_LEFT_ARROW    0xD8

Suggestion, it is a lot easier to read code when you use named things like this versus magic numbers like 216...
Especially when different processors may use different magic numbers.

On teensy this is defined as:
Code:
#define KEY_LEFT                (  80  | 0xF000 )
#define KEY_LEFT_ARROW	KEY_LEFT

I believe the main issue is that doing something like: Keyboard.write(KEY_LEFT);

Will not works, as this simply translates to: trying to: Keyboard.write(80); or Keyboard.write('P');

Which is not what is wanted.

You need to instead use: Keyboard.press(KEY_LEFT); which understands the encoding.
 
Looking at the code (it's been quite a while since I wrote it...) the short answer is to use Keyboard.press() and Keyboard.release() rather than Keyboard.write().

The longer answer is Teensy's Keyboard.print() and Keyboard.write() are using bytes 128-255 for UTF8 encoding. So you can't access special non-printing keys like the arrows, page up/down, scroll lock, etc with Keyboard.write(). Teensy's Keyboard can be configured to various layouts, where it translates these 2 or 3 byte sequences into the key or key combination the configured keyboard layout would use to type those special chars. This is mainly meant so you can use Keyboard.print() with strings that have international characters, but because print() is built on top of write(), it also means the input to write() is UTF8.

To make that Arduino code work on Teensy, just replace Keyboard.write() with Keyboard.press() followed by Keyboard.release(). And use the names like KEY_UP rather than the number, since Teensy supports the same key names as Arduino, but the actual numbers used are different.
 
Thanks for the replies, and my apologies for the duplicate thread. I have a lot into this project, and it represents my first foray into microprocessors and Arduino coding. As I learned to do all of this -- a fairly steep curve -- I relied upon numerous tutorials and code examples from the Arduino world. I'm not a coder by trade and I've been developing a very specific project, learning as I go. The project is adding Bluetooth capability to a vintage manual typewriter. I developed a sensor bar that accurately reads 42 discrete analog inputs using a linear resistor I built in the shop (a blend of graphite and epoxy resin). I was very pleased when I got it all working -- flawlessly -- with the Leonardo. My impression was that the Teensy would be easily implemented, was smaller (something I needed), and more powerful. Not only that, but the Leonardo is a disaster when coding from a Windows machine because it just doesn't play nicely when it comes to USB ports, and I finally moved over to a Linux box to avoid all of that. The Teensy works and uploads flawlessly in Windows, a big plus. My mistake was in assuming (never a good idea) that what works on the Leonardo (HID) would translate directly to Teensy (HID).
Believe it or not there is a market for such a machine, having to do with a lot of writers starting to use vintage manual typewriters for first drafts (it's part of a thing called "distraction free writing". Typewriters are becoming big again, oddly enough. I'll post a link here about the machine I've built: https://youtu.be/mPXegGU7qsY
 
Indeed a really nice project. If you publish any info about using Teensy, even just a quick glance at the electronics, let me know. We could show it on the website with a link to your sales page.
 
Back
Top