Catching SHIFT + CTRL at the same time

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
mikegl
Earned a small fee
Earned a small fee
Posts: 14
Joined: Fri Jul 12, 2019 12:31 am

Catching SHIFT + CTRL at the same time

Post by mikegl » Wed Aug 28, 2019 11:54 pm

Hello, I am trying to catch the key commands (events) SHIFT + CONTROL at the same time to set a flag. While I am executing the following code,
it is not working. Trying to catch the SHIFT and CTRL commands separately it works fine, but that is not what I want to achieve.

Code: Select all

if ((event.GetKeyCode() == WXK_SHIFT) && (event.GetModifiers() == wxACCEL_CTRL))
	mbUpDownCam = true;
else
	mbUpDownCam = false;
I have done this with CTRL + F10 before the above and it works well. Both methods are implemented in the OnKeyDown method
in GLCanvas.

Code: Select all

if ((event.GetKeyCode() == WXK_F10) && (event.GetModifiers() == wxACCEL_CTRL)) { ... }

User avatar
doublemax
Moderator
Moderator
Posts: 13985
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Catching SHIFT + CTRL at the same time

Post by doublemax » Thu Aug 29, 2019 12:13 am

Using wxACCEL_CTRL here is definitely wrong. It just happens to work because it has the same value as wxMOD_CONTROL.

Use the example code from here:
https://docs.wxwidgets.org/trunk/classw ... 5a447e4aa5
Use the source, Luke!

mikegl
Earned a small fee
Earned a small fee
Posts: 14
Joined: Fri Jul 12, 2019 12:31 am

Re: Catching SHIFT + CTRL at the same time

Post by mikegl » Thu Aug 29, 2019 12:24 pm

Thank you, I have changed all to wxMOD_..., but the following code is still not working. I have tried to change the event.GetKeyCode() == WXK_SHIFT with event.GetModifiers() == wxMOD_SHIFT..., too.

Code: Select all

if ((event.GetKeyCode() == WXK_SHIFT) && (event.GetModifiers() == wxMOD_CONTROL))
	mbUpDownCam = true;
else
	mbUpDownCam = false;
By the way, when do I use the wxACCEL_... commands?

User avatar
doublemax
Moderator
Moderator
Posts: 13985
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Catching SHIFT + CTRL at the same time

Post by doublemax » Thu Aug 29, 2019 1:07 pm

Which key event are you catching here anyway?

Have you tried something like this?

Code: Select all

if ( event.ControlDown() && event.ShiftDown() )
By the way, when do I use the wxACCEL_... commands?
For wxAcceleratorTable:
https://docs.wxwidgets.org/trunk/classw ... table.html
Use the source, Luke!

mikegl
Earned a small fee
Earned a small fee
Posts: 14
Joined: Fri Jul 12, 2019 12:31 am

Re: Catching SHIFT + CTRL at the same time

Post by mikegl » Thu Aug 29, 2019 1:31 pm

I am trying to catch the WXK_SHIFT + wxMOD_CONTROL key event at the same time.
Have you tried something like this? if ( event.ControlDown() && event.ShiftDown() )
No, I didn't because here https://docs.wxwidgets.org/trunk/classw ... 5a447e4aa5 stands that:
Notice that this function is easier to use correctly than, for example, ControlDown() because when using the latter you also have to remember to test that none of the other modifiers is pressed:
By the way after reading the ControlDown explanation here: https://docs.wxwidgets.org/trunk/classw ... 79106b18dd
The ControlDown method is no more recommended to use.
Returns true if the Control key or Apple/Command key under OS X is pressed.
This function doesn't distinguish between right and left control keys.
Notice that GetModifiers() should usually be used instead of this one.
But your solution works :). Thanks

User avatar
doublemax
Moderator
Moderator
Posts: 13985
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Catching SHIFT + CTRL at the same time

Post by doublemax » Thu Aug 29, 2019 3:03 pm

Code: Select all

if( event.GetModifiers() == (wxMOD_CONTROL | wxMOD_SHIFT) )
This should work, too. GetModifiers returns a bit field with different bits set for each modifier.
Use the source, Luke!

mikegl
Earned a small fee
Earned a small fee
Posts: 14
Joined: Fri Jul 12, 2019 12:31 am

Re: Catching SHIFT + CTRL at the same time

Post by mikegl » Thu Aug 29, 2019 3:27 pm

Yes, this worked, too. Great thank you very much =D>

Post Reply