产生键盘事件击键

发布于 2024-07-26 20:03:59 字数 135 浏览 1 评论 0原文

如何编写一个简单的 C 程序来产生键盘按键。

if ( condition ) {
    KeyPress('A');
}

我正在 Ubuntu 8.10 Linux 操作系统上工作

How to make a simple C program which will produce keyboard key hits.

if ( condition ) {
    KeyPress('A');
}

I am working on Ubuntu 8.10 Linux OS

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

笙痞 2024-08-02 20:03:59

这是一个使用 libxdo(来自 xdotool)的简单示例。 (警告:我是 xdotool 的作者)

 /* File: testkey.c
 *
 * Compile with:
 * gcc -lxdo testkey.c
 *
 * Requires libxdo (from xdotool project)
 */

#include <xdo.h>

int main() {
  xdo_t *xdo = xdo_new(NULL);
  xdo_keysequence(xdo, CURRENTWINDOW, "A", 0);
  return  0;
}

Here's a simple example using libxdo (from xdotool). (Caveat: I am the xdotool author)

 /* File: testkey.c
 *
 * Compile with:
 * gcc -lxdo testkey.c
 *
 * Requires libxdo (from xdotool project)
 */

#include <xdo.h>

int main() {
  xdo_t *xdo = xdo_new(NULL);
  xdo_keysequence(xdo, CURRENTWINDOW, "A", 0);
  return  0;
}
孤城病女 2024-08-02 20:03:59

查看 xsendkey。 包含的源代码很短,因此您可以从中提取必要的部分到您的程序中。

Take a look at xsendkey. The sources are included and are short, so you extract the necessary parts from it into your program.

感情旳空白 2024-08-02 20:03:59

虽然这不是 C,但您可以很容易地在 Java 中生成按键:

import java.awt.Robot;
import java.awt.AWTException;
import java.awt.event.KeyEvent;


public class key
{
    public static void main(String args[])
    {
        try {
            Robot r = new Robot();
            r.delay(2000);
            r.keyPress(KeyEvent.VK_W);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }
};

Although this is not C, you can produce key hits in Java very easily:

import java.awt.Robot;
import java.awt.AWTException;
import java.awt.event.KeyEvent;


public class key
{
    public static void main(String args[])
    {
        try {
            Robot r = new Robot();
            r.delay(2000);
            r.keyPress(KeyEvent.VK_W);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }
};
我家小可爱 2024-08-02 20:03:59

看看 Swinput

Swinput 可以伪造鼠标和
使用 Linux 输入法的键盘
系统。 swinput 模块读取
设备和假硬件事件
(鼠标移动、按键等)作为
在设备上写入的命令。

Have a look at Swinput.

Swinput can fake a mouse and a
keyboard by using the Linux Input
System. The swinput modules read from
a device and fakes hardware event
(mouse motion, key presses etc) as
commands written on the devices.

紫﹏色ふ单纯 2024-08-02 20:03:59

通过 Xdotool 获取假按键事件

//Compile As:  gcc button.c -lX11 

#include < X11/Xlib.h >
#include < X11/Xutil.h >
#include < stdio.h >
#include < X11/extensions/XTest.h >

void press_button()
{   
    Display *d;
    d = XOpenDisplay(NULL);
        if(d == NULL)
        {
            //fprintf(stderr, "Errore nell'apertura del Display !!!\n");
            //exit(0);
        }
    system("xdotool key Shift+a");
    XFlush(d);
    XCloseDisplay(d);
}

int main() {
    press_button();
    return 0;
}

Get Fake Key Events by Xdotool

//Compile As:  gcc button.c -lX11 

#include < X11/Xlib.h >
#include < X11/Xutil.h >
#include < stdio.h >
#include < X11/extensions/XTest.h >

void press_button()
{   
    Display *d;
    d = XOpenDisplay(NULL);
        if(d == NULL)
        {
            //fprintf(stderr, "Errore nell'apertura del Display !!!\n");
            //exit(0);
        }
    system("xdotool key Shift+a");
    XFlush(d);
    XCloseDisplay(d);
}

int main() {
    press_button();
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文