在最近的项目中,需要在目标板上实现键盘快捷键的双击调用操作菜单。 在此,我们将在qml提供的Shortcut快捷方式控件的模拟中实现
创建qml组件:CustomKeyEvent.qml
importqtquick 2.0 importqtquick 2.12 item { id : keyitempropertyvarcustomkey 3360 qstr (' tab ' ) } propertyintispressed 3360 propertyintcustomkeydoubleclick : anchors.fill : parent focus 3360 true timer { id 3360 keytimeminnning 00 on triggered : { key item.is pressed=0} shortcut { sequence : customkeyautorepeat 3360 false//context : Qt.applicationshortcutonactivated : { console.log (customkey ' activate ' ) dClick ) } } function dClick ) 长按不触发{if(1===keyitem.ispressed )//key item.is pressed=0key item.customkeydoubleclick=1key time.stop
importqtquick 2.12 importqtquick.window 2.12 importqtquick.controls 2.12 window { id : win visible 3360 true width : screen.wind button { id : testbtnanchors.center in : parent background : rectangle { color : ' orange ' opacity 3360.2 width 3360 } text : qstr (测试按钮)自定义密钥(自定义密钥: qstr (空间) ) )。 oncustomkeydoubleclickchanged : { if (1==customkeydoubleclick ) testBTN.text=qstr ) testbuttondoubleclick.' else rectangle (anchors.fill : parent color 3360 ) #00ffee ) opacity3360
# includeqguiapplication # includeqqmlapplicationengineintmain (int argc,char *argv[] () ) qcoreapplication 33603360 seta TTA QQmlApplicationEngine engine; 控制字符串(qrc :/main.qml ); qobject:3360connect(engine,qqmlapplicationengine :3360 object created,app,[URL] ) qobject*obj,constqurlobj 、Qt :3360排队连接; engine.load(URL; 返回app.exec (; }