【问题标题】:Keyboard.print() equivalent for C on Teensy?在 Teensy 上相当于 C 的 Keyboard.print()?
【发布时间】:2016-01-19 18:14:04
【问题描述】:

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

【问题讨论】:

  • 从一本好的 C 书开始。
  • @Olaf 这是什么意思?除了显而易见的......我的意思是,如果你愿意,我可以称它为 char 数组 :)
  • 开始阅读吧。你会发现你的问题在那里得到了解答。如果我现在告诉你使用什么,这将导致相当多的后续问题。所以最好自己开始学习。教钓鱼与授鱼。
  • @Olaf 我猜那是“不”。没关系,我可以自己写。我只是希望能更节省内存。
  • 哪个青少年版? Teensy++ 2.0 及更早版本是 AVR,可以在裸机上使用 avr-gcc 或在 Arduino IDE 中使用teensyduino 进行编程; Teensy 3.0、3.1、3.2 和 LC 具有 ARM 内核,所有支持库都需要使用 Arduino IDE。请记住,Arduino 草图只是带有一些自动源按摩的 C++;你也可以在 Arduino IDE 中编写 C++。

标签: c arduino teensy


【解决方案1】:

你的问题不太清楚,所以我已经回答了我从中得到的两种解释。

如果您指的是使用 Teensy 作为输入设备。

是的,如果您在所选操作系统下使用teensyduino,所有 Teensy 设备都应该可以做到这一点。只需输入您的代码并设置 Teensy as a HID device 即可使用它。
重申一下好的建议,请小心测试,因为流氓键盘很快会给您带来很多麻烦。

如果您指的是让 C 程序模拟键盘。

是的,这也是可能的。

Linux 说明

Example Code - Found on SO
该示例使用#define KEYCODE XK_Down,这是“安全的”,但很难显示。尝试在适当的位置添加#define KEYCODE XK_A#include <unistd.h>usleep(2000000); // 2 seconds 以控制行为。
此外,如果您需要 C 而不是 C++,请将 createKeyEvent 中的 Window &win 更改为 Window win

修改下面的代码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
我觉得很勇敢,还没有测试过,所以抓住你的裤子。

【讨论】:

    猜你喜欢
    • 2020-02-25
    • 2012-09-30
    • 1970-01-01
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多