问题描述
我正在使用面向UWP的Windows WRL的C ++语言进行编码,我需要处理鼠标输入,有时需要同时按下向左/向右按钮。
我的代码仅收到一个事件,该事件涉及按下的第一个鼠标按钮和释放的最后一个鼠标按钮。因此,如果我先按向右按钮,再按向左按钮,则不会为左按钮触发Pointerpressed。同样,如果我释放右按钮然后释放左按钮,则不会为右按钮触发PointerReleased。我订阅了以下事件:
void App::Setwindow(CoreWindow^ window)
{
window->Pointerpressed += ref new TypedEventHandler<CoreWindow ^,PointerEventArgs ^>(this,&App::OnPointerpressed);
window->PointerReleased += ref new TypedEventHandler<CoreWindow ^,&App::OnPointerReleased);
}
void App::OnPointerpressed(CoreWindow ^sender,PointerEventArgs ^args)
{
}
void App::OnPointerReleased(CoreWindow ^sender,PointerEventArgs ^args)
{
}
请让我知道我的代码是否需要在按钮按下/释放之间重置某些内容,或者我是否应该订阅其他事件。
谢谢
解决方法
在交互会话中检测到的第一次鼠标按键按下会触发CoreWindow.PointerPressed事件,而在交互会话结束之前,第二次按下鼠标键不会触发该事件。同样,CoreWindow.PointerReleased事件仅在释放最后一个鼠标按钮时才触发,即,在释放第一个按钮而第二个按钮不释放时不触发该事件。
因此,您不能使用CoreWindow.PointerPressed
和CoreWindow.PointerReleased
事件来检测同时按下左右按钮。
您可以使用CoreWindow.PointerMoved事件来检测您同时进行的两个鼠标操作。
例如,
Void App::SetWindow(CoreWindow^ window)
{
window->PointerMoved+=ref new TypedEventHandler<CoreWindow^,PointerEventArgs^>(this,&App::OnPointerMoved);
}
Void App::OnPointerMoved(CoreWindow^ sender,PointerEventArgs^ args)
{
if (args->CurrentPoint->Properties->IsLeftButtonPressed)
{
pressed++; // pressed is an int variable whose initial value is 0,pressed is defind in App.xaml.h
}
if (args->CurrentPoint->Properties->IsRightButtonPressed)
{
released++; // released is an int variable whose initial value is 0,released is defind in App.xaml.h
}
if (pressed&&released)
{
pressed = 0;
released = 0;
//Here we know the right and left button of mouse is pressed
}
}