2

I'm trying to write a program for Teensy that uses it as a keyboard. I want it to print out certain strings depending on input, but some of the strings are rather long. I know that for Arduino IDE there's a simple Keyboard.print() function that allows the sending of strings. Does this functionality exist for C as well?

Teensy
  • 21
  • 1
  • Start with a good C book. – too honest for this site Oct 20 '15 at 22:19
  • @Olaf what does that mean? Besides the obvious... I mean, I can call it a char array if you want :) – Teensy Oct 20 '15 at 22:27
  • Just start reading. YOu willfind your question answered there. If I would tell you now what to use, this will lead to quite some follow-up questions. So it's better you start learnig yourself. Teaching fishing vs. giving a fish. – too honest for this site Oct 20 '15 at 22:48
  • @Olaf I'm guessing that's a "no." That's OK, I can write it myself. I was just hoping to be a bit more memory-efficient. – Teensy Oct 20 '15 at 22:55
  • Which Teensy version? Teensy++ 2.0 and earlier are AVRs and can be programmed using avr-gcc on bare metal or in Arduino IDE using [teensyduino](http://www.pjrc.com/teensy/teensyduino.html); Teensy 3.0, 3.1, 3.2 and LC have ARM cores and all the support libraries require using the Arduino IDE. Remember, Arduino sketches are just C++ with a bit of automatic source massaging thrown in; you can just write C++ in Arduino IDE, too. – Nominal Animal Oct 21 '15 at 03:14

1 Answers1

0

Your question is rather unclear, so I've answered both interpretations I got from it.

If you are referring to using a Teensy as an input device.

Yes, this should be possible with all Teensy units if you use teensyduino under the OS of your choice. Simply put in your code and set the Teensy as a HID device to use it.
To restate good advice, be careful testing as a rogue keyboard can quickly cause a lot of trouble for you.

If you are referring to getting a C program to emulate a keyboard.

Yes, this is also possible.

Linux Instructions

Example Code - Found on SO
The example uses #define KEYCODE XK_Down, which is "safe", but can be hard to show. Try adding #define KEYCODE XK_A, #include <unistd.h>, and usleep(2000000); // 2 seconds in appropriate places to control the behavior.
Additionally, if you need C instead of C++, change Window &win to Window win in createKeyEvent

Modified code below XFakeKey.c

// Send a fake keystroke event to an X window.
// by Adam Pierce - http://www.doctort.org/adam/
// This is public domain software. It is free to use by anyone for any purpose.

// debug message, delay, and conversion to C by ti7
// original code found at http://www.doctort.org/adam/nerd-notes/x11-fake-keypress-event.html

#include <X11/Xlib.h>
#include <X11/keysym.h>

// The key code to be sent.
// A full list of available codes can be found in /usr/include/X11/keysymdef.h
//#define KEYCODE XK_Down
#define KEYCODE XK_A

// Function to create a keyboard event
XKeyEvent createKeyEvent(Display *display, Window win,
                           Window winRoot, int press,
                           int keycode, int modifiers)
{
   XKeyEvent event;

   event.display     = display;
   event.window      = win;
   event.root        = winRoot;
   event.subwindow   = None;
   event.time        = CurrentTime;
   event.x           = 1;
   event.y           = 1;
   event.x_root      = 1;
   event.y_root      = 1;
   event.same_screen = True;
   event.keycode     = XKeysymToKeycode(display, keycode);
   event.state       = modifiers;

   if(press)
      event.type = KeyPress;
   else
      event.type = KeyRelease;

   return event;
}

#define true 1
#define false 0

#include <stdio.h> // for printf
#include <unistd.h> // for time
main()
{
// Obtain the X11 display.
   Display *display = XOpenDisplay(0);
   if(display == NULL)
      return -1;
    printf("it's working\n");
// Get the root window for the current display.
   Window winRoot = XDefaultRootWindow(display);

    usleep(1000000);
// Find the window which has the current keyboard focus.
   Window winFocus;
   int    revert;
   XGetInputFocus(display, &winFocus, &revert);

// Send a fake key press event to the window.
   XKeyEvent event = createKeyEvent(display, winFocus, winRoot, true, KEYCODE, 0);
   XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);

// Send a fake key release event to the window.
   event = createKeyEvent(display, winFocus, winRoot, false, KEYCODE, 0);
   XSendEvent(event.display, event.window, True, KeyPressMask, (XEvent *)&event);

// Done.
   XCloseDisplay(display);
   return 0;
}

Compile with gcc -o XFakeKey XFakeKey.c -L/usr/X11R6/lib -lX11

Windows Example

Example Code
I'm feeling brave and have not tested this, so hold on to your pants.

Community
  • 1
  • 1
ti7
  • 16,375
  • 6
  • 40
  • 68