我正在尝试为 Teensy 编写一个将其用作键盘的程序。我希望它根据输入打印出某些字符串,但有些字符串相当长。我知道对于 Arduino IDE,有一个简单的Keyboard.print()
函数可以发送字符串。C 语言也有这个功能吗?
1 回答
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.