linux下模拟键盘输入
有时候我们需要模拟键盘输入,在windows下,有 keybd_event 模拟键盘按下。在linux下可以使用Xlib提供的接口去模拟键盘按下。 代码如下:
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
void ClickKey()
{
Display* p_display = XOpenDisplay( NULL );
KeySym keysym = XK_X;
KeyCode keycode = NoSymbol;
keycode = XKeysymToKeycode( p_display , keysym );
XTestFakeKeyEvent( p_display , keycode , True , 0 );
XTestFakeKeyEvent( p_display , keycode , False , 0 );
XFlush( p_display );
XCloseDisplay( p_display );
}
int main(){
ClickKey();
}
需要链接-lX11 -lXtst. 编译指令: g++ main.cpp -o main -lX11 -lXtst.
应用
模拟键盘输入可以应用在pcl框选点云中,我们知道pcl款选事件默认是按下键盘"X"才可以款选点云,我们可以模拟键盘按下“X”,这样就可以不用按下"X"了。代码如下:
#include <pcl/io/pcd_io.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <iostream>
#include <thread>
#include <vector>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer(new pcl::visualization::PCLVisualizer("viewer"));
pcl::PointCloud<pcl::PointXYZ>::Ptr clicked_points_3d(new pcl::PointCloud<pcl::PointXYZ>);
int num = 0;
void pp_callback(const pcl::visualization::AreaPickingEvent &event, void *args) {
std::vector<int> indices;
if (event.getPointsIndices(indices) == -1)
return;
for (int i = 0; i < indices.size(); ++i) {
clicked_points_3d->points.push_back(cloud->points.at(indices[i]));
}
pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> red(clicked_points_3d, 255, 0, 0);
std::stringstream ss;
std::string cloudName;
ss << num++;
ss >> cloudName;
cloudName += "_cloudName";
viewer->addPointCloud(clicked_points_3d, red, cloudName);
viewer->setPointCloudRenderingProperties(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, 10, cloudName);
Display *p_display = XOpenDisplay(NULL);
KeySym keysym = XK_X;
KeyCode keycode = NoSymbol;
keycode = XKeysymToKeycode(p_display, keysym);
XTestFakeKeyEvent(p_display, keycode, True, 0);
XTestFakeKeyEvent(p_display, keycode, False, 0);
XFlush(p_display);
}
int main() {
if (pcl::io::loadPCDFile("/home/ls/Downloads/code/wr_ls_udp/points23.pcd", *cloud)) {
std::cerr << "ERROR: Cannot open file " << std::endl;
return -1;
}
viewer->addPointCloud(cloud, "bunny");
viewer->setCameraPosition(0, 0, -2, 0, -1, 0, 0);
viewer->registerAreaPickingCallback(pp_callback, (void *) &cloud);
Display *p_display = XOpenDisplay(nullptr);
KeySym keysym = XK_X;
KeyCode keycode = NoSymbol;
keycode = XKeysymToKeycode(p_display, keysym);
XTestFakeKeyEvent(p_display, keycode, True, 0);
XTestFakeKeyEvent(p_display, keycode, False, 0);
XFlush(p_display);
while (!viewer->wasStopped()) {
viewer->spinOnce(100);
std::this_thread::sleep_for(std::chrono::microseconds(1));
}
}
CMakeLists.txt如下:
cmake_minimum_required(VERSION 3.10)
project(test)
set(CMAKE_CXX_STANDARD 14)
FIND_PACKAGE(PCL REQUIRED)
#pcl
include_directories(${PCL_INCLUDE_DIRS})
# 设置依赖库链接目录
link_directories(${PCL_LIBRARY_DIRS})
# 添加预处理器和编译器标记
add_definitions(${PCL_DEFINITIONS})
add_executable(pcl_keyborad pcl_keyborad.cpp)
target_link_libraries(pcl_keyborad -lX11 -lXtst ${PCL_LIBRARIES} -lpthread)
其他应用
模拟键盘按下事件结合pcl点云以及可以在Qt中有很多用处。
参考链接
1.Simulate keypress in a Linux C console application: https://stackoverflow.com/questions/1262310/simulate-keypress-in-a-linux-c-console-application Xlib Linux下模拟键盘按下:https://www.cnblogs.com/chaichengxun/p/15425727.html
|