Creating a usb keyboard with Teensy 4.1 and using it on windows

alvise

Member
First of all, hello everyone.
I currently have teensy 4.1.
I ran the sample application on the homepage. I can type any character on the computer, but I cannot use it like a normal keyboard in some applications. For example, it cannot be used in labVIEW. The pressed button is not visible there.
I suspect the root of this problem is not creating an instance that can work exactly as a usb keyboard.
Does anyone have a sample code for this that I can compile with the arduino editor?
 
Sorry,

But hard to say... Not a lot of information on what you have tried or have not tried...

You can type a key on what?
You mention home page, is it this page? https://www.pjrc.com/teensy/td_keyboard.html
And the program?
Code:
int count = 0;
void setup() { } // no setup needed
void loop() {
  Keyboard.print("Hello World "); 
  Keyboard.println(count);
  count = count + 1;
  delay(5000);
}

I also have never used LabVIEW so cannot comment on what it needs.

When you build with USB Type that includes Keyboard, you are a USB keyboard. But I don't know what LabVIEW wants or its limitations.

The main way I have experimented with the USB type including Keyboard, is in conjunction with USBHost, where I am forwarding everything from the keyboard to the host.
That sample is under USBHost_t36 as KeyboardForward
 
Teensy's USB keyboard interface uses the same protocol as virtually all USB keyboards (top-tier gaming keyboards being the exception). The only real difference is the speed of "typing".

Maybe Labview is looking at HID key down and key up events with its relatively slow processing loop?

Keyboard.print() transmits the HID events at the USB interrupt endpoint polling rate. Maybe that's much faster than Labview is able to recognize?
 
Thanks for your answers.
I tested this below code and everything works fine.I just want to use 10 buttons that can be pressed at the same time, how should I go about it?
Do I need to add all the buttons to an array and use it like this?

Code:
#include<Keyboard.h>
#define button1 2
void setup() {

  pinMode(button1, INPUT_PULLUP);
  //pinMode(3, INPUT_PULLUP);
  Keyboard.begin();
}

void loop() {

  if (digitalRead(button1) == LOW) {
  Keyboard.press(97);
    delay(300);  
// 
Keyboard.release(97);
  }
}
 
I am pretty sure 10 buttons at once would not be supported.

That is the Keyboard works in "Boot" mode, that is the mode that is known most all setups that use USB Keyboards.

These messages are typically 8 bytes long:
First byte holds state about what modifier keys are down.
Second byte is mostly ignored.
The last 6 can hold scan codes of which keys are pressed, 0 filled to end if less than 6...
Note Some keyboards look like they only support 4 at time...

However, there are some keyboards who support N key rollover, that can handle more than 6, in a variety of ways.
I added some support recently into USB Host code to be able to handle input from some of these keyboards.

But I am pretty sure the USB Type that includes keyboard will not support this.

As for code for handling multiple buttons, I would probably use something like the debounce library to make sure that you dont get a lot of press/release.
And I would probably not do the delays for each key unless you don't mind waiting that long between presses if you are trying to handle more than one button down at a
time.

It is also unclear from your code versus your statement, if you wish for the button to be pushed and released on each down of the button, or if you wish for the key to be released when the
button is released?
 
I just want to use 10 buttons that can be pressed at the same time,

Only 6 can be pressed at the same time, plus the 4 modifier keys (shift, ctrl, alt, windows/cmd).

Nearly all USB keyboards have this limitation. They all use the same protocol which supports only 6 simultaneous keys, plus the modifiers. As Kurt mentioned, some special gaming keyboards support more, but they are fairly rare. Almost all normal USB keyboards use the 6-key protocol, which Teensy also uses so that it appears to a PC as a normal USB keyboard.


how should I go about it?

Just use Keyboard.press(); 6 times for the keys you want pressed together. Don't forget to later use Keyboard.release().
 
thanks for your answers.
Actually, I want to create a button box, not a keyboard, but I found it more logical to appear as a keyboard on the computer. Because it can run smoothly on almost any operating system.

If I insist on the possibility of pressing all 10 buttons, is there an alternative solution?
-here are some game controllers that can be seen on the computer without the need for drivers or anything else, and they are in the HID class.mouse,joyctick,etc
-Note: I'm thinking of using it on some permanent buttons.
Is it possible to create something like this?
If possible, can I find an example?
 
I came here to get help with Teensy keyboard I've created. I'm thinking that I need to either add a udev rule or supply power to the device. After programming it I was able to use it once, but after this it didn't work. So Either I fried it or something else is going on (which is what I think).
I can maybe help with the questions brought up here though. If you want to use a teensy 4.1 to send multiple keystrokes, yes this chip can do it. Better than any other device I've seen because it does not require a matrix and can independently handle different signals. That all depends on how it's wired up, of course. I don't know if there is some limitation to keypresses on the OS side, I don't see how USB comes into play... if it does I'm not aware of the limitation. You probably want to use the keydown method not print.
 
Back
Top