2015-08-28 92 views
1

我的环境是Windows 10 64位上的Visual Studio 2013。如何检测键盘上特殊键的向下/向上事件

在我的Windows Store应用(适用于Windows 8.1),我附加这样的键盘事件(这是一个C++/CX计划,因为我使用的是C++工具包):

auto amv = Windows::ApplicationModel::Core::CoreApplication::MainView; 
if (amv){ 
    auto cw = amv->CoreWindow; 
    if (cw){ 
     cw->KeyDown += ref new TypedEventHandler<CoreWindow ^, KeyEventArgs^>(srt, &WinRTApp::OnKeyDown); 
     cw->KeyUp += ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(srt, &WinRTApp::OnKeyUp); 
    } 
} 

当我按下我的日语(109)键盘上的Hankaku键。系统使用未定义的VirtualKey代码(243)和代码为244的KeyDown事件触发KeyUp事件。并且当我释放该密钥时,没有事件触发。

第二次按键触发KeyUp(244)和KeyDown(243),第二次释放没有触发。

我想精确检测KeyUp事件。有什么好方法吗?

回答

-1

我查看了你的问题,发现了一个相当简单的方法来处理每次不管字符值的关键事件。在Onlaunched事件中,您可以在App.xaml.cpp中添加事件处理程序,也可以将其添加到特定页面,例如。 MainPage.xaml.cpp

Windows::ApplicationModel::Core::CoreApplication::MainView->CoreWindow->KeyUp += ref new Windows::Foundation::TypedEventHandler<Windows::UI::Core::CoreWindow ^, Windows::UI::Core::KeyEventArgs ^>(this, &KeyUpTest::App::OnKeyUp); 

在事件处理程序本身只需使用

void KeyUpTest::App::OnKeyUp(Windows::UI::Core::CoreWindow ^sender,  Windows::UI::Core::KeyEventArgs ^args) 
{ 

} 

这将触发每一个非系统的关键了发生的时间。您可以对bool数组使用相同的进程来处理多个键状态。

更多信息可以在这里找到:http://www.cplusplus.com/forum/windows/117293/

有许多与KEYUP问题,的KeyDown命令是他们返回按下的键的值,而不是人物的选择的值,例如:

如果我按7,响应是55,如果我按shift 7,答案仍然是55而不是&的值。您可能正在寻找的是CharacterReceived事件,它提供更多的上下文并处理大小写和其他值。

要添加此事件中,你可以使用

Windows::ApplicationModel::Core::CoreApplication::MainView->CoreWindow->CharacterReceived += ref new Windows::Foundation::TypedEventHandler<Windows::UI::Core::CoreWindow ^, Windows::UI::Core::CharacterReceivedEventArgs ^>(this, &KeyUpTest::App::OnCharacterReceived); 

而且处理程序是这样的:

void KeyUpTest::App::OnCharacterReceived(Windows::UI::Core::CoreWindow ^sender, Windows::UI::Core::CharacterReceivedEventArgs ^args) 
{ 
    bool iskey = false; 
    int keycode = args->KeyCode; 

    if (keycode == 65) { 
     iskey = true; 
    } 
} 

我希望这是有帮助的。

+0

字母键没有问题。 Hankaku的关键不会通过该解决方案工作。 – Tank2005

相关问题