我的目标是让一个程序在后台休眠,但可以由用户通过一些"热键"激活.从挖掘Xlib手册和Xlib O'reilly手册开始,我认为正确的方法是使用XGrabKey.但是我对这个过程的理解是不正确的,因为简单的概念证明不起作用.
我的理解是,如果我叫XGrabKey与根窗口作为grab_window和owner_events假,每当我的热键按下事件,然后将发送只到根窗口.如果我然后从根窗口选择KeyPress事件,然后侦听X事件,我应该在按下热键时获得按键事件.我在下面粘贴了一个最小的例子.
我期望的是,当程序运行时,无论哪个窗口有焦点,如果按下Ctrl + Shift + K,我的程序应输出"热键被按下!" 在控制台中,然后终止.
此外,我的理解是,如果XGrabKey失败,默认错误处理程序将显示一条消息,因为它不是我假设调用成功.
显然,我的理解在某种程度上是有缺陷的.谁能指出我正确的方向?
#include <iostream>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
using namespace std;
int main()
{
Display* dpy = XOpenDisplay(0);
Window root = DefaultRootWindow(dpy);
XEvent ev;
unsigned int modifiers = ControlMask | ShiftMask;
int keycode = XKeysymToKeycode(dpy,XK_Y);
Window grab_window = root;
Bool owner_events = False;
int pointer_mode = GrabModeAsync;
int keyboard_mode = GrabModeAsync;
XGrabKey(dpy, keycode, modifiers, grab_window, owner_events, pointer_mode,
keyboard_mode);
XSelectInput(dpy, root, KeyPressMask );
while(true)
{
bool shouldQuit = false;
XNextEvent(dpy, …Run Code Online (Sandbox Code Playgroud) 我有一台运行Linux/X11的嵌入式设备连接到通过USB连接提供触摸事件的设备.此设备无法识别为任何形式的标准指针/鼠标输入.我正在尝试做的是找到一种方法,在外部设备报告事件时将鼠标事件"注入"X11.
这样做将不再需要我的应用程序(使用Gtk +用C语言编写)来伪造用Gtk +调用的鼠标.
如果可以这样做,我的Gtk +应用程序将不需要知道或关心生成触摸事件的设备.它只会在应用程序中显示为标准鼠标事件.
谁知道如何将合成鼠标事件插入X11?
现在我正在做以下工作,但不是最佳的.
GtkWidget *btnSpin; /* sample button */
gboolean buttonPress_cb( void *btn );
gboolean buttonDePress_cb( void *btn );
/* make this call after the device library calls the TouchEvent_cb() callback
and the application has determined which, if any, button was touched
In this example we are assuming btnSpin was touched.
This function will, in 5ms, begin the process of causing the button to do it's
normal animation ( button in, button out effects ) …Run Code Online (Sandbox Code Playgroud) 我经历了答案,并遇到了两种方法,可以帮助区分扫描仪和键盘输入。可以通过以下方式完成:
基于时间:扫描仪输入比手动键盘输入更快。
基于前缀:在条形码或扫描仪(扫描仪设备内置)中添加前缀,并使用它来识别扫描仪输入。
我遇到的问题是,在触发扫描事件时,只要用户手动键入一些键盘键,就会将其添加到扫描仪输入中,并导致结果不一致。
这是我正在使用的代码:
var BarcodeScannerEvents = function(){
this.initialize.apply(this, arguments);
};
BarcodeScannerEvents.prototype = {
initialize: function() {
$(document).on({
keypress: $.proxy(this._keypress, this)
});
},
_timeoutHandler: 0,
_inputString: '',
_keypress: function (e){
if(this._timeoutHandler){
clearTimeout(this._timeoutHandler);
}
this._inputString += String.fromCharCode(e.which);
//CHECKS FOR VALID CHARACTERS WHILE SCANNING
this._timeoutHandler = setTimeout($.proxy(function(){
if(this._inputString.length <= 10){
this._inputString = '';
return;
}
$(document).trigger('barcodescanned', this._inputString);
this._inputString = '';
}, this), 20);
}
};
new BarcodeScannerEvents();
Run Code Online (Sandbox Code Playgroud)
我的条形码格式为:〜xxx-xxx-xxxxxx,其中x可以是0-9之间的任何数字。如果将数字字符附加到条形码上,则会导致数据库中的错误插入。
我曾尝试比较键盘输入和扫描仪输入的事件,但无济于事。我考虑过在每个数字前添加额外的字符,然后如果出现连续数字,则使扫描的条形码无效。但是我认为这不是解决此问题的最佳方法。有人可以帮我吗?
我尝试编写一个程序,它挂钩键盘消息,在Ubuntu(KDE)中按下时发出每个键的名称; 不干扰程序中键盘的正常操作(只是宣布键名).
这是我的计划:
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
void SendPressKeyEvent(Display *display, XKeyEvent xkey)
{
Window current_focus_window;
int current_focus_revert;
XGetInputFocus(display, ¤t_focus_window, ¤t_focus_revert);
xkey.type = KeyPress;
xkey.display = display;
xkey.window = current_focus_window;
xkey.root = DefaultRootWindow(display);
xkey.subwindow = None;
xkey.time = 1000 * time(0);
xkey.x = 0;
xkey.y = 0;
xkey.x_root = 0;
xkey.y_root = 0;
xkey.same_screen = True;
XSendEvent(display, InputFocus, True, KeyPressMask, (XEvent *)(&xkey));
}
void SendReleaseKeyEvent(Display *display, XKeyEvent xkey)
{
Window current_focus_window;
int …Run Code Online (Sandbox Code Playgroud)