我为基于触摸的平板电脑开发了一些软件。该装置没有键盘,只有触摸屏(触摸屏模拟鼠标)。操作系统是 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]
总而言之,您需要:
- 从
PointPickingCallback
派生一个新类,它会覆盖Execute
方法中的 shift 键检查。 - 派生自
PCLVisualizerInteractorStyle
并覆盖Initialize
方法以注册新的自定义PointPickingCallback
类,该类不检查 shift 键,如鼠标回调。 - 将新的交互器样式传递给
PCLVisualizer
。
值得注意。可视化器支持两种选择模式:用于区域选择的橡皮筋和单点拾取,可以通过按“x”键来互换。我认为默认选择的是橡皮筋,这就是为什么它可能首先检查是否按下了 shift 键。
关于c++ - 如何伪造键盘换档键。 Qt,Windows 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49230562/