wxEVT_CHAR works, wxEVT_KEY_UP does not

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
mael15
Super wx Problem Solver
Super wx Problem Solver
Posts: 464
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

wxEVT_CHAR works, wxEVT_KEY_UP does not

Post by mael15 » Thu Apr 15, 2021 4:36 pm

Hello everyone,
I have a wxScrolledWindow populated with wxStaticBitmaps and the selected bitmap is painted with a blue edge. The user can select a bitmap by clicking on it and I want to achieve that pressing the delete key deletes the bitmap. Now, in the bitmaps ctor i use

Code: Select all

Bind(wxEVT_CHAR, &ScrollerBitmap::onKeyUp, this);
and when painting the blue edge I do

Code: Select all

SetFocus();
ScrollerBitmap::onKeyUp is hit when the user hits the delete key, but I want it to only fire once. The problem is: wxEVT_KEY_UP does not work, nothing happens.
What could be wrong?

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 5085
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxEVT_CHAR works, wxEVT_KEY_UP does not

Post by ONEEYEMAN » Thu Apr 15, 2021 5:54 pm

Hi,
Can't you do the binding inside the mouse click handler and then unbind the event at the end of the function?

Also - try to use wxEVT_KEY_DOWN, not _UP.

Thank you.

mael15
Super wx Problem Solver
Super wx Problem Solver
Posts: 464
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: wxEVT_CHAR works, wxEVT_KEY_UP does not

Post by mael15 » Fri Apr 16, 2021 2:01 pm

thanx!
as i understand it the mouse down event also fires as long as the key is pressed? i guess that would be trouble since i want to delete the item that triggers the deletion?
i do not understand why it should be better to bind inside the mouse click handler?

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 5085
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxEVT_CHAR works, wxEVT_KEY_UP does not

Post by ONEEYEMAN » Fri Apr 16, 2021 3:16 pm

Hi,
Not necessarily.

Those 2 events are independent - you can click the mouse, press the key or do both at the same time.

But it shouldn't matter anyway - you don't need the "Del" key binding without the selection, right? So do just that in your program. And then when you delete what you don't want - unbind the event. Accidents can happen and it is better not to allow user to make the mistake than catch it.

It will be better in the long run.

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

Re: wxEVT_CHAR works, wxEVT_KEY_UP does not

Post by doublemax » Fri Apr 16, 2021 5:15 pm

Is this all about avoiding to delete multiple items because of the key-repeat of the delete key?

Under Windows you can get this information from wxKeyEvent::GetRawKeyFlags(). If bit 30 is set, the event is the result of an auto-repeat.

Code: Select all

bool isRepeat = (keyevent.GetRawKeyFlags() & 0x40000000) != 0;
Use the source, Luke!

mael15
Super wx Problem Solver
Super wx Problem Solver
Posts: 464
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: wxEVT_CHAR works, wxEVT_KEY_UP does not

Post by mael15 » Sun Apr 18, 2021 7:57 am

doublemax wrote:
Fri Apr 16, 2021 5:15 pm

Code: Select all

bool isRepeat = (keyevent.GetRawKeyFlags() & 0x40000000) != 0;
That is a nice trick, and it helps!
I am wondering why a "simple and clean" EVT_KEY_UP is not triggered while EVT_CHAR is. Any idea?

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

Re: wxEVT_CHAR works, wxEVT_KEY_UP does not

Post by doublemax » Sun Apr 18, 2021 8:28 am

mael15 wrote:
Sun Apr 18, 2021 7:57 am
I am wondering why a "simple and clean" EVT_KEY_UP is not triggered while EVT_CHAR is. Any idea?
Not really. Got any sample code i can play around with? I'm lazy on a sunday ;)
Use the source, Luke!

mael15
Super wx Problem Solver
Super wx Problem Solver
Posts: 464
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: wxEVT_CHAR works, wxEVT_KEY_UP does not

Post by mael15 » Sun Apr 18, 2021 10:03 am

doublemax wrote:
Sun Apr 18, 2021 8:28 am
Not really. Got any sample code i can play around with? I'm lazy on a sunday ;)
I tried, but my code is so integrated that it is too much work. thank you for offering help though. :)

Post Reply