简体   繁体   English

如何在 wxWidgets 中创建合成键盘事件以触发在 wxAcceleratorEntry 中设置的快捷方式?

[英]How to create a synthetic keyboard event in wxWidgets to trigger shortcuts set in a wxAcceleratorEntry?

I am using wxWebView in my application.我在我的应用程序中使用 wxWebView。 Since this widget consumes all keyboard events internally, I have to create a synthetic keyboard event and process it.由于此小部件在内部使用所有键盘事件,因此我必须创建一个合成键盘事件并对其进行处理。 This is the code that I am using for creating a synthetic keyboard event:这是我用于创建合成键盘事件的代码:

        // create a synthetic keyboard event and handle it
        wxKeyEvent keyEvent( wxEVT_KEY_DOWN );
        keyEvent.SetEventObject( ctrl_ );
        auto key = url.substr( keyCodePrefix_.length() );
        if( key == "Escape" )
          keyEvent.m_keyCode = WXK_ESCAPE;
        else if( key == "F1" )
          keyEvent.m_keyCode = WXK_F1;
        else
          keyEvent.m_keyCode = WXK_NONE; 
        ctrl_->ProcessWindowEvent( keyEvent );

As you could see, I only handle Escape and F1 keys for now.如您所见,我现在只处理EscapeF1键。 The type of keyboard event that I am using is wxEVT_KEY_DOWN .我使用的键盘事件类型是wxEVT_KEY_DOWN Everything works fine.一切正常。 According to the doc, the keyboard is processed in the widget then is sent to the application.根据文档,键盘在小部件中处理,然后发送到应用程序。 However it does not trigger the shortcuts are set in the parent window ( that contains wxWebView widget ) viawxAcceleratorTable .但是它不会触发通过wxAcceleratorTable在父 window(包含 wxWebView 小部件)中设置的快捷方式。

How should I create a keyboard event that trigger shortcuts in my accelerator table?我应该如何创建一个键盘事件来触发我的快捷键表中的快捷方式?

I tried to set the type of keyboard event to wxEVT_CHAR but it also did not work.我试图将键盘事件的类型设置为wxEVT_CHAR但它也没有用。

Update: my event handler is like below:更新:我的事件处理程序如下所示:

class MyApp : public wxApp
{
  public:
    MyApp();
    bool OnInit() override;
    // ...

    bool ProcessEvent(wxEvent& event) override
    {
      if( event.GetEventType() == wxEVT_KEY_DOWN )
      {
        wxKeyEvent& ke = (wxKeyEvent&)event;
        if( ke.GetKeyCode() == WXK_ESCAPE )
        {
         // handle keyboard event
        }
        event.Skip(); // this does not help!
      }
      return wxApp::ProcessEvent( event );
    }

    // ...    
    DECLARE_EVENT_TABLE()
};

Then I think the only option will be to have some way of calling the method the accelerator entry would trigger.然后我认为唯一的选择是有一些方法来调用加速器条目将触发的方法。

I used sendInput function of Win32 to create a synthetic key down event.我使用 Win32 的sendInput function 来创建一个合成按键按下事件。 It is exactly emulating keyboard events which means it triggers the shortcuts in accelerator table.它完全模拟键盘事件,这意味着它会触发加速器表中的快捷方式。 Also be noticed that the keyboard event goes to the focused widget.另请注意,键盘事件会转到焦点小部件。

    INPUT inputs[ 2 ] = {};
    ZeroMemory( inputs, sizeof( inputs ) );
    auto key = url.substr( keyCodePrefix_.length() );
    if( key == "Escape" )
    {
      // key down
      inputs[ 0 ].type = INPUT_KEYBOARD;
      inputs[ 0 ].ki.wVk = VK_ESCAPE;
      // key up
      /*inputs[ 1 ].type = INPUT_KEYBOARD;
      inputs[ 1 ].ki.wVk = VK_ESCAPE;
      inputs[ 1 ].ki.dwFlags = KEYEVENTF_KEYUP;*/
    }
    else if( key == "F1" )
    {
      // key down
      inputs[ 0 ].type = INPUT_KEYBOARD;
      inputs[ 0 ].ki.wVk = VK_F1;
      // key up
      /*inputs[ 1 ].type = INPUT_KEYBOARD;
      inputs[ 1 ].ki.wVk = VK_F1;
      inputs[ 1 ].ki.dwFlags = KEYEVENTF_KEYUP;*/
    }
    SendInput( ARRAYSIZE( inputs ), inputs, sizeof( INPUT ) );
    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM