c++ - 如何伪造键盘换档键。 Qt,Windows 10

标签 c++ windows qt point-cloud-library

我为基于触摸的平板电脑开发了一些软件。该装置没有键盘,只有触摸屏(触摸屏模拟鼠标)。操作系统是 Windows 10,我们使用 Qt 作为图形用户界面框架。

我们依赖于一个类库点云库,它有一个组件需要按下 SHIFT 键才能在我们单击鼠标左键时发生某些事情。

我需要让底层软件组件相信已经按下了 SHIFT 键。

我试图通过“Qt 方法”将按键事件发送到 Qt 小部件,但假的 SHIFT 按键似乎没有到达底层 sw。成分。可能是因为它位于与 Qt 无关的类库中,并且该软件类库可能以不同方式检查关键事件(例如通过操作系统调用、c++ std 方式或类似方式)。

尝试“Qt 方式”时这似乎不起作用:

QKeyEvent key_press(QEvent::KeyPress, Qt::Key_Shift, Qt::ShiftModifier);
QApplication::sendEvent(ui->qvtkWidget, &key_press);

因此我可能需要使用操作系统方法来伪造 SHIFT 键按下。

问题: 我如何才能从 Qt 执行操作系统系统调用或类似操作,以使底层软件组件认为已按下并按住 SHIFT 键(同时用户按下鼠标左键)。

最佳答案

您想要的不是伪造 shift 键输入,而是在交互级别禁用 shift 检查。绝对是 XY 问题。

vtk InteractorStyle 类负责处理用户输入事件。您可以在 line 506 中看到pcl_visualizer.cpp 的

boost::signals2::connection
pcl::visualization::PCLVisualizer::registerPointPickingCallback (boost::function<void (const pcl::visualization::PointPickingEvent&)> callback)
{
  return (style_->registerPointPickingCallback (callback));
}

回调在 PCLVisualizerInteractorStyle 类中注册。

您可以在构造 PCLVisualizer 时传递您自己的自定义交互器样式,这样您就可以安全地覆盖它。要使用的构造函数是这个 one

PCLVisualizer (int &argc, char **argv, const std::string &name="", PCLVisualizerInteractorStyle *style=PCLVisualizerInteractorStyle::New(), const bool create_interactor=true) 

PointPickingCallback 类正在 added automatically作为初始化默认交互器样式时的鼠标回调。因此,为了覆盖此行为,您需要从 PCLVisualizerInteractorStyle 派生您自己的类并覆盖 Initialize() 方法并将这个新的交互器样式传递给 PCLVisualizer 构建时。

Execute PointPickingCallback 中的方法是检查 shift 键的地方。

void
pcl::visualization::PointPickingCallback::Execute (vtkObject *caller, unsigned long eventid, void*)
{
  PCLVisualizerInteractorStyle *style = reinterpret_cast<PCLVisualizerInteractorStyle*>(caller);
  vtkRenderWindowInteractor* iren = reinterpret_cast<pcl::visualization::PCLVisualizerInteractorStyle*>(caller)->GetInteractor ();
  if (style->CurrentMode == 0)
  {
    if ((eventid == vtkCommand::LeftButtonPressEvent) && (iren->GetShiftKey () > 0))
    {
      float x = 0, y = 0, z = 0;
      int idx = performSinglePick (iren, x, y, z);
      // Create a PointPickingEvent if a point was selected   
      [... and so on]

总而言之,您需要:

  1. PointPickingCallback 派生一个新类,它会覆盖 Execute 方法中的 shift 键检查。
  2. 派生自 PCLVisualizerInteractorStyle 并覆盖 Initialize 方法以注册新的自定义 PointPickingCallback 类,该类不检查 shift 键,如鼠标回调。
  3. 将新的交互器样式传递给 PCLVisualizer

值得注意。可视化器支持两种选择模式:用于区域选择的橡皮筋和单点拾取,可以通过按“x”键来互换。我认为默认选择的是橡皮筋,这就是为什么它可能首先检查是否按下了 shift 键。

关于c++ - 如何伪造键盘换档键。 Qt,Windows 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49230562/

相关文章:

c++ - 小部件与它的 child 大小一样吗?

c++ - QTcpServer::incomingConnection(qintptr socketDescriptor) 是否可以连接指定的套接字?

c++ - 为什么 .c_str() 不在字符串末尾添加 '\0' ?

c++ - 对象数组的选择排序

c++ - 如何在 Windows 上使用 C++ 测量 CPU 时间并包括 system() 的调用?

windows - 为什么 UiPath 检测到的 ctrlid 格式与其 XSLT 表示中记录的格式不同?

.net - WCF Windows 服务超时

c++ - 加密PP : how to use SocketSource and SocketSink

c++ - 即使 V(U) 有效,从 std::pair<T, U> 到 std::pair<T, V> 的转换也不起作用?

c++ - Qt 我可以在构造函数中将信号/槽连接到自身吗?