简体   繁体   English

键盘事件无法正常运行

[英]Keybord events do not work properly

I have created a game which works fine but I have a problem with the key commands part. 我创建了一个运行良好的游戏,但键盘命令部分存在问题。 When I click somewhere on the screen my KeyDown event does not work. 当我单击屏幕上的某处时,我的KeyDown事件不起作用。 I guessed that the control which has the event handler for KeyDown loses the focus and my application was not getting the message. 我猜想具有KeyDown事件处理程序的控件失去了焦点,而我的应用程序未收到消息。 I have spent one week trying to solve the problem. 我已经花了一个星期的时间来解决这个问题。

I tried to set the KeyDown event for more controls. 我试图为更多控件设置KeyDown事件。 I also tried to set the focus of the control which handles the events without success. 我还尝试设置处理事件未成功的控件的焦点。

I would appreciate if someone tell me what is the right way of handling the key press collection in my game which will work whatever happens in my application. 如果有人告诉我在游戏中处理按键集合的正确方法是什么,无论在应用程序中发生什么情况,该方法都能正常工作,我将不胜感激。 I just want to know when the user pressed the arrow keys in order to change the contents. 我只想知道用户何时按下箭头键以更改内容。

我只是遇到了类似的问题,但是随后我在LayoutAwarePage.CoreDispatcher_AcceleratorKeyActivated中迷上了他们的工作方式,它似乎按预期工作了。

I am not sure about WinRT but in WPF, you can catch routed events from all child controls by adding a handler like this. 我不确定WinRT,但在WPF中,您可以通过添加这样的处理程序来捕获所有子控件的路由事件。

this.AddHandler(UIElement.PreviewKeyDownEvent, new RoutedEventHandler(OnKeyDown), true);

Where this is parent control. 这是父控件。 It could work for WinRT too, please try 它也适用于WinRT,请尝试

EDIT 编辑

replace UIElement.PreviewKeyDownEvent with UIElement.KeyDownEvent if UIElement.PreviewKeyDownEvent doesn't exists in WinRT 更换UIElement.PreviewKeyDownEventUIElement.KeyDownEvent如果UIElement.PreviewKeyDownEvent不存在的WinRT

I did not answered earlier because the system did not allow me to make it because I did not have 10 reputation points so I had to wait 8 hours. 我没有早些回答,因为系统不允许我这样做,因为我没有10个信誉点,所以我不得不等待8个小时。

This is what I typed but it seems that it needs to declare something there that I do not know how to make it. 这是我键入的内容,但似乎需要在其中声明一些我不知道如何制作的内容。

this.AddHandler(UIElement.KeyUpEvent, new RoutedEventHandler(KeyUp), true);

The error message is 错误消息是

Delegate Windows.UI.Xaml.RoutedEventHandler Represents the method that will handle routed events. 委托Windows.UI.Xaml.RoutedEventHandler表示将处理路由事件的方法。 Error: No overload for 'KeyUp' matches delegate 'Windows.UI.Xaml.RoutedEventHandler. 错误:“ KeyUp”没有重载匹配代理“ Windows.UI.Xaml.RoutedEventHandler”。

The function which will work as event handler has the following format: 可用作事件处理程序的函数具有以下格式:

private void KeyUp(object sender, KeyRoutedEventArgs e)
{
}

I found a way to bypass my problem and I would like to share it with you just in case someone else needs it but if someone else knows a better way to make let me know. 我找到了一种绕过我的问题的方法,并且希望与您分享,以防万一其他人需要它,但是如果其他人知道让我知道的更好的方法。

I placed a button out of the visible area of the page and I used it's KeyUp event and I wrote code to make sure that it is always active. 我在页面的可见区域外放置了一个按钮,并使用了它的KeyUp事件,并编写了代码以确保它始终处于活动状态。 Below you can find my code. 在下面可以找到我的代码。

public frmMain()
{
    this.InitializeComponent();

    btnKeboardCollector.Loaded += MyBoard_Loaded;
    btnKeboardCollector.LostFocus += btnKeboardCollector_LostFocus;
    btnKeboardCollector.KeyUp += KeyUpHandler;
}

void MyBoard_Loaded(object sender, RoutedEventArgs e)
{
    // I do other initialization here

    btnKeboardCollector.Focus(Windows.UI.Xaml.FocusState.Programmatic);
}

void btnKeboardCollector_LostFocus(object sender, RoutedEventArgs e)
{
    btnKeboardCollector.Focus(Windows.UI.Xaml.FocusState.Programmatic);
}

private void KeyUpHandler(object sender, KeyRoutedEventArgs e)
{
}

I've just found a very straight forward solution to this. 我刚刚找到了一个非常简单的解决方案。 It always works, whether an element is focused or not. 无论元素是否集中,它始终有效。

1st: the function 第一:功能

private void onAcceleratorKey(object sender, AcceleratorKeyEventArgs e)
{
    CoreVirtualKeyStates ctrl = Window.Current.CoreWindow.GetAsyncKeyState(VirtualKey.Control);

    if (ctrl == CoreVirtualKeyStates.Down)
    {
        switch (e.VirtualKey)
        {
            case VirtualKey.F:
                // CTRL + F pressed.
                break;
        }
    }
}

2nd: the event 第二名:活动

Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated += onAcceleratorKey;

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

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