Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 17 of 17

Thread: USB keyboard hardware proxy

  1. #1
    Junior Member
    Join Date
    Apr 2018
    Posts
    7

    USB keyboard hardware proxy

    What I want to build: a device into which I can plug in my keyboard and that will look like a USB keyboard if I plug it into a USB host of any kind that accepts a keyboard. The idea is for it to be all in hardware, such that no driver software is needed on the host. By default it'd proxy all key presses/releases, but of course I want to do some interesting things, like record, replay, maybe swap keyboard maps, etc. I'll want a few buttons on this gizmo. And optionally maybe have a small LCD display to echo key sequences (with an assumed keyboard layout).

    So obviously, I need to implement a USB host and a USB HID device. Even if this were feasible in an Arduino, the ATMega328's 2k of RAM seem a bit tight for the recording ability I'm interested in.

    Is the Teensy (3.2) an adequate Microcontroller for this?

    And what else will I need? I came across a page explaining how it can be paired with the Arduino USB host shield. Is the shield necessary, or can the Teensy (with whatever other needed hardware, e.g. USB-A jack and pulldown resistors) be programmed to handle this directly? As for the USB device interface, should I use the micro-USB port on the Teensy card for it, or add a new one?

    Reading up on it, I realized that the trickiest thing for a USB device will be power management, and properly reporting needs to the USB host. Also, the page explaining the USB host shield points out that the power demand when plugging in the keyboard will cause the Teensy to reboot if I don't put a capacitor there. But if the device is plugged in to the computer before to the keyboard is plugged in to the device, it won't be able to properly estimate maximum power demand to the host, so I'm thinking maybe that reboot is a good thing, and then presumably the computer will see us as a new device and request power needs anew, which we should be able to report? Incidentally, what are the power needs of the Teensy card itself?

    Note: I have no direct experience with Microcontroller programing, this would be my first such project. But I have some digital design experience (PALs, CPLDs, with VHDL) from back in the day, know the basics of how keyboards and keyboard drivers work (scan codes, clear codes, etc.), and I'm fluent in C.

    Thanks in advance for your insights!

  2. #2
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    5,679
    The Teensy 3.6 has two USB Ports, one is a HOST and you can connect a keyboard. The other can be programmed to act as a keyboard easyly. So, in short: Yes, possible. Of course, you have to write your software to program the Teensy 3.6

  3. #3
    Junior Member
    Join Date
    Apr 2018
    Posts
    7
    Quote Originally Posted by Frank B View Post
    The Teensy 3.6 has two USB Ports, one is a HOST and you can connect a keyboard. The other can be programmed to act as a keyboard easyly. So, in short: Yes, possible. Of course, you have to write your software to program the Teensy 3.6
    Thanks for the info. Fair enough, but at that price, a Raspberry Pi would also do the trick, wouldn't it?...
    Perhaps a bit more power-hungry, though.

  4. #4
    Senior Member
    Join Date
    Apr 2013
    Posts
    1,883
    There are USB host modules that communicate via SPI, have libraries and do sometimes function, so would be possible on a T3.2. Trick is getting a working USB host module. See adventures here
    https://forum.pjrc.com/threads/43357...eld-(chineese)

  5. #5
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    5,679
    Quote Originally Posted by GremlinWrangler View Post
    There are USB host modules that communicate via SPI, have libraries and do sometimes function, so would be possible on a T3.2. Trick is getting a working USB host module. See adventures here
    https://forum.pjrc.com/threads/43357...eld-(chineese)
    Or just use a PS/2 Keyboard and connect it to the Teensy 3.2

  6. #6
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,021
    I think he wants to make a logger, where teensy takes a physically keyboard plugged into it, and teensy plugged to PC. Teensy would then pass-through all keystrokes while logging them to.. uSD?

  7. #7
    Senior Member
    Join Date
    Feb 2016
    Location
    Australia
    Posts
    215
    Maybe wants a hardware keyboard macro device for games that monitor for software macros. (I've heard they do this but could be rubbish).

  8. #8
    Senior Member
    Join Date
    Apr 2013
    Posts
    1,883
    @gibbedy - many moons ago tried to use a Leonardo variant to play a game that required complex 4-8 button combos and got detected - looks like it recorded the timings and started dropping the tail of combos if the timing was too close to perfect.

    Dithering the timing might have helped but didn't care enough at the time to try that just to finish a game I was terrible at.

  9. #9
    Junior Member
    Join Date
    Apr 2018
    Posts
    7
    Heh, yeah, I've heard of macro-detection, but rest assured: my macros are for comms only. I want this mainly for an Xbox, and it only allows keyboard use for messages. I want macros to quickly send message for mic-less comms. E.g. "Hi Cmdr, this is a hold-up. Kindly kill your speed and leave hardpoints closed for a quick scan. I will merely ask for a modest amount of your cargo, and if you comply, you will be on your way unharmed." If I have to manually type something like that, the victim will be long since gone.

    Now, if you wanted to make game macros on an xbox, I wonder if one could intercept a wired controller? And I suspect some randomness for the interval between keystrokes would be enough to evade detection - is that what you mean by dithering? But, not relevant to me.

  10. #10
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,021
    you could add voice recognition to that optionally

  11. #11
    Junior Member
    Join Date
    Apr 2018
    Posts
    7
    Taking a look at the Teensy 3.6, it does seem to be the right controller for the job.

    But what about the power management issues? How would you approach the various initialization scenarios I outlined above? (device plugged in first, then keyboard into device, or the other way around) How about suspend mode? Keepalive? Turning the LCD on/off? How would the controller measure its own power usage? (Does it need to?) Some trick with a pull-up resistor or pull-down resistor to an analog input? I haven't really thought it through...

  12. #12
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,021
    teensy doesnt need a keyboard, it can act as one standalone, you can use a button on gpio to fire your message to the console

  13. #13
    Junior Member
    Join Date
    Apr 2018
    Posts
    7
    Quote Originally Posted by tonton81 View Post
    teensy doesnt need a keyboard, it can act as one standalone, you can use a button on gpio to fire your message to the console
    Right, but that's rather limited, I have something more elaborate in mind. That message was just one example usage. I want to record several strings, be able to edit them, etc. For one, I don't want to limit myself to 2kB of RAM. I want a polished-looking product in the end, several buttons to select functions. The keyboard will still be needed, this is still only a proxy. If I wanted to have a standalone macro keypad, that exists already commercially, and is easier to DIY.

    What I want to make is similar to this: https://making.arantius.com/remapcro, but not quite; in some ways more, in other ways, simpler. Don't really want to go into details as I haven't nailed them down and would prefer to share when I have a finished product or something close to it.

    As for the voice recognition idea... I know you probably meant that tongue-in-cheek, but... hmm.

  14. #14
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,021
    yeah thats a good idea, you can use macro keys to add phrases without reprogramming teensy, theyd be stored on uSD. would be nice to have a little lcd to touch the ones you dont want, to delete them, or a touch to add a new one

  15. #15
    Junior Member
    Join Date
    Apr 2018
    Posts
    7
    So, working through the USBHost_t36 library, I'm trying to figure out the simplest way to implement my proxy. I will record keys using attachPress() and attachRelease(), easy enough. But I would also like to forward all keyboard reports messages seamlessly from the host to the device. I'm looking at changing the library directly, at least just to make KeyboardController::new_data() virtual, so I can override it. Can't really think of an easier way... can anyone else?
    Last edited by Hooman; 05-21-2018 at 05:06 AM. Reason: formatting

  16. #16
    Member
    Join Date
    Jul 2018
    Posts
    38
    Looks like there are pull requests to make it easy to implement a USB keyboard pass through/proxy. One is for USB keyboard host side modifications and the other for device side.

    https://github.com/PaulStoffregen/USBHost_t36/pull/18
    https://github.com/PaulStoffregen/cores/pull/268

    I hacked a pass through but this looks more complete although I do not understand the need for some of the other changes.

  17. #17
    Member
    Join Date
    Jul 2018
    Posts
    38
    A simple example of a USB keyboard pass through. I verified it works on a Xbox One.

    Code:
    // derived from "mouse" example of USBHost_t36 library
    // simple relaying of keystrokes between a keyboard connected to the
    // usb host port of a teensy 3.6 and the output of the teensy
    // acting as a usb keyboard
    
    /*
      The Teensy 3.6 has a USB host port as well as the usual USB device port
      so it can act as a USB pass through or proxy device.
    
      The pull request below (not mine) allows a sketch to receive the
      USB keyboard HID report when it arrives from the USB host port.
      This version does not require changes to Keyboard.
    
      Latest usbhost_t36 from git
      https://github.com/PaulStoffregen/USBHost_t36
    
      with this pull request applied
      https://github.com/PaulStoffregen/USBHost_t36/pull/18
    
      Hardware
        Teensy 3.6 board
        https://www.pjrc.com/store/teensy36.html
        Teensy 3.6 USB host cable
        https://www.pjrc.com/store/cable_usb_host_t36.html
    */
    
    #include "USBHost_t36.h"  // to host a usb keyboard
    #include "Keyboard.h"     // to act as a usb keyboard device
    
    USBHost myusb;
    KeyboardController keyboard_in(myusb);
    
    /*
      Key remap possiblities
        Swap LeftCtrl and CapsLock. Put CapsLock in the corner where it belongs!
        Switchable QWERTY, Dvorak, and colemak keyboard layouts
        Hardware key macros
        Remappings and macros that work even in BIOS and recovery mode.
        Remappings and macros that work with KVMs and game consoles.
    */
    void reportReader(const uint8_t report[8])
    {
      Keyboard.set_modifier(report[0]);
      Keyboard.set_key1(report[2]);
      Keyboard.set_key2(report[3]);
      Keyboard.set_key3(report[4]);
      Keyboard.set_key4(report[5]);
      Keyboard.set_key5(report[6]);
      Keyboard.set_key6(report[7]);
      Keyboard.send_now();
    }
    
    void setup()
    {
      myusb.begin();
      keyboard_in.attachReportReader(reportReader);
    }
    
    void loop() {
      myusb.Task();
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •