模拟键preSS在Linux下C控制台应用程序 [英] Simulate keypress in a Linux C console application

查看:144
本文介绍了模拟键preSS在Linux下C控制台应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法用C来模拟Linux的一个关键preSS?

Is there any way to simulate a keypress in Linux using C?

在我的具体情况,我在Ubuntu 9.04,需要一个简单的应用程序启动时调用的暂停按钮preSS。这将获得在Firefox中的iframe使用JavaScript来刷新。

In my specific situation, I'm on Ubuntu 9.04 and need a simple app that invokes a press on the "pause" button when launched. That would get an iframe in Firefox to refresh using Javascript.

推荐答案

我假设你的意思是X11应用程序 - 这是不是从你的描述你打算做完全清楚。下面code段将发送暂停键code到当前具有使用XTEST扩展X11下的键盘输入焦点中的应用 - 从我读过这是最兼容的方式,以打假键盘事件。看看你是否会将此到您的场景(没有错误检查对XOpenDisplay是否成功,使其更简单)。

I assume you mean the "X11 application" - it is not entirely clear from your description what you are planning to do. The below code snippet will send the "pause" keycode to the application that currently has the keyboard input focus under X11 using XTest extension - from what I've read this is the most compatible way to "fake" the keyboard events. See if you might apply this to your scenario (no error check on whether the XOpenDisplay succeeded, to make it simpler).

#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
...
Display *display;
unsigned int keycode;
display = XOpenDisplay(NULL);
...
keycode = XKeysymToKeycode(display, XK_Pause);
XTestFakeKeyEvent(display, keycode, True, 0);
XTestFakeKeyEvent(display, keycode, False, 0);
XFlush(display);

您需要用-lX11 -lXtst链接。

You will need to link with the -lX11 -lXtst.

显然火狐将需要在那个时候有焦点。

Obviously the firefox would need to have focus at that time.

不过,我会很好奇,想知道什么是更重要的任务,你所要完成的 - 我怀疑应该比欺骗的关键preSS事件更优雅的解决方案。

However, I would be curious to know what is the bigger task that you are trying to accomplish - I suspect there should be a more elegant solution than spoofing the keypress events.

这篇关于模拟键preSS在Linux下C控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆