我试图为Teensy编写一个用它作为键盘的程序。我希望它根据输入打印出某些字符串,但有些字符串相当长。我知道对于Arduino IDE,有一个简单的Keyboard.print()
函数允许发送字符串。此功能是否也适用于C?
答案 0 :(得分:0)
你的问题还不清楚,所以我已经回答了我从中得到的两种解释。
是的,如果您在所选操作系统下使用teensyduino,则所有Teensy设备都应该可以使用。只需输入您的代码并设置Teensy as a HID device即可使用它 为了重申好的建议,小心测试作为流氓键盘会很快给你带来很多麻烦。
是的,这也是可能的。
Linux说明
Example Code - Found on SO
该示例使用#define KEYCODE XK_Down
,这是“安全的”,但很难显示。尝试在适当的位置添加#define KEYCODE XK_A
,#include <unistd.h>
和usleep(2000000); // 2 seconds
来控制行为
此外,如果您需要C而不是C ++,请在Window &win
Window win
更改为createKeyEvent
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;
}
使用gcc -o XFakeKey XFakeKey.c -L/usr/X11R6/lib -lX11
Windows示例
Example Code
我感觉很勇敢,没有测试过,所以坚持穿裤子。