UWP C++ 鼠标按钮向上事件无法始终如一地工作
Posted
技术标签:
【中文标题】UWP C++ 鼠标按钮向上事件无法始终如一地工作【英文标题】:UWP C++ Mouse Button Up Events Not Working Consistently 【发布时间】:2021-06-27 19:33:41 【问题描述】:OnPointerPressed 工作正常,但 OnPointerReleased 不行。
window->KeyDown +=
ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &EngineMain::OnKeyPressed);
window->KeyUp +=
ref new TypedEventHandler<CoreWindow^, KeyEventArgs^>(this, &EngineMain::OnKeyReleased);
window->PointerPressed +=
ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &EngineMain::OnPointerPressed);
window->PointerReleased +=
ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &EngineMain::OnPointerReleased);
window->PointerMoved +=
ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &EngineMain::OnPointerMoved);
void EngineMain::OnPointerPressed(CoreWindow^ sender, PointerEventArgs^ args)
if (args->CurrentPoint->Properties->IsLeftButtonPressed)
m_soundManager->GetSound(L"./Assets/rifle.wav")->StartSound();
args->Handled = true;
if (args->CurrentPoint->Properties->IsRightButtonPressed)
m_soundManager->GetSound(L"./Assets/shotgun.wav")->StartSound();
args->Handled = true;
void EngineMain::OnPointerReleased(CoreWindow^ sender, PointerEventArgs^ args)
Windows::UI::Core::CoreVirtualKeyStates lmb = sender->GetKeyState(Windows::System::VirtualKey::LeftButton);
Windows::UI::Core::CoreVirtualKeyStates mmb = sender->GetKeyState(Windows::System::VirtualKey::MiddleButton);
Windows::UI::Core::CoreVirtualKeyStates rmb = sender->GetKeyState(Windows::System::VirtualKey::RightButton);
if (lmb == Windows::UI::Core::CoreVirtualKeyStates::Down)
m_soundManager->GetSound(L"./Assets/rifle.wav")->StartSound();
args->Handled = true;
if (rmb == Windows::UI::Core::CoreVirtualKeyStates::Down)
m_soundManager->GetSound(L"./Assets/shotgun.wav")->StartSound();
args->Handled = true;
我确实注意到点击的模式。如下:
鼠标按下有声音,鼠标松开有声音,鼠标按下有声音,鼠标松开没有声音
然后它重复 OR
鼠标按下发出声音,鼠标松开没有声音,鼠标按下发出声音,鼠标松开发出声音
这些天的鼠标事件似乎非常复杂。任何帮助将不胜感激,甚至可能是如何为鼠标按钮设置正确 OnPointerReleased 事件的编码示例。键盘事件是正确的。已发布事件的 MSDN 完全失败,因为它没有告诉您如何检查已发布的按钮。
【问题讨论】:
【参考方案1】:你可以尝试将代码m_soundManager->GetSound(L"./Assets/rifle.wav")->StartSound();
放在OnPointerReleased
方法的前面,可以查看鼠标松开时播放的声音,即每次调用OnPointerReleased
方法鼠标释放。我了解到您想确定在 OnPointerReleased
方法上释放了哪个鼠标按钮。您可以尝试两种方法。
一种方法是改变if语句的条件,例如:
void UpEvent::MainPage::OnPointerReleased(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args)
args->Handled = true;
Windows::UI::Core::CoreVirtualKeyStates lmb = sender->GetKeyState(Windows::System::VirtualKey::LeftButton);
Windows::UI::Core::CoreVirtualKeyStates mmb = sender->GetKeyState(Windows::System::VirtualKey::MiddleButton);
Windows::UI::Core::CoreVirtualKeyStates rmb = sender->GetKeyState(Windows::System::VirtualKey::RightButton);
if (lmb != Windows::UI::Core::CoreVirtualKeyStates::None)
m_soundManager->GetSound(L"./Assets/rifle.wav")->StartSound();
if (rmb != Windows::UI::Core::CoreVirtualKeyStates::None)
m_soundManager->GetSound(L"./Assets/shotgun.wav")->StartSound();
另一种方法是在 OnPointerPressed 方法中而不是在 OnPointerReleased 方法中获取鼠标按钮信息。
int mouseButton=-1;
void UpEvent::MainPage::OnPointerPressed(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args)
if (args->CurrentPoint->Properties->IsLeftButtonPressed)
……
mouseButton = 0;
if (args->CurrentPoint->Properties->IsRightButtonPressed)
……
mouseButton = 1;
void UpEvent::MainPage::OnPointerReleased(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args)
args->Handled = true;
if (mouseButton == 0)
//Left mouse button is released
m_soundManager->GetSound(L"./Assets/rifle.wav")->StartSound();
if (mouseButton == 1)
//Right mouse button is released
m_soundManager->GetSound(L"./Assets/shotgun.wav")->StartSound();
【讨论】:
【参考方案2】:我解决了自己的问题,并认为离开该职位是个好主意。您实际上需要使用布尔值并在 OnPointerReleased 期间重置它们,因为发送者和参数不准确。用布尔值来做:
void EngineMain::OnPointerPressed(CoreWindow^ sender, PointerEventArgs^ args)
if (args->CurrentPoint->Properties->IsLeftButtonPressed)
m_mouseDevice->m_keyMap[DI::CMouseDevice::LMB]->m_isDown = true;
if (args->CurrentPoint->Properties->IsMiddleButtonPressed)
m_mouseDevice->m_keyMap[DI::CMouseDevice::MMB]->m_isDown = true;
if (args->CurrentPoint->Properties->IsRightButtonPressed)
m_mouseDevice->m_keyMap[DI::CMouseDevice::RMB]->m_isDown = true;
void EngineMain::OnPointerReleased(CoreWindow^ sender, PointerEventArgs^ args)
m_mouseDevice->ResetButtons();
m_mouseDevice->ResetButtons();将所有布尔值重置为 false。虽然这只是一次一个按钮的策略,但由于不准确,它似乎是唯一的方法。最后一点,OnPointerPressed 仅在按下按钮时发生一次,因此如果您需要处理被按住的按钮,请这样做。
【讨论】:
以上是关于UWP C++ 鼠标按钮向上事件无法始终如一地工作的主要内容,如果未能解决你的问题,请参考以下文章
actionscript 3-按钮操作[侦听多个事件。[鼠标移过,鼠标移出,鼠标向上]