wxSlider Processing Problem With wxKeyEvent (ding)

Are you writing your own components and need help with how to set them up or have questions about the components you are deriving from ? Ask them here.
Post Reply
Xangis
Earned some good credits
Earned some good credits
Posts: 122
Joined: Fri Apr 14, 2006 9:49 pm
Location: Beaverton, OR
Contact:

wxSlider Processing Problem With wxKeyEvent (ding)

Post by Xangis » Fri Jan 29, 2010 4:29 pm

I have a custom control, a wxBitmapSlider derived from wxSlider. It allows an optional custom bitmap for the slider and thumb.

I have key down and key up events attached to this control.

Code: Select all

	_modWheel = new wxBitmapSlider( itemDialog1, ID_MODWHEEL, 0, 0, 16383, wxDefaultPosition, wxSize( 30, 108 ), wxSL_VERTICAL|wxSL_INVERSE );
	_modWheel->Connect(ID_MODWHEEL, wxEVT_KEY_DOWN, wxKeyEventHandler(wxKeyboard::OnKeyDown), NULL, this);
	_modWheel->Connect(ID_MODWHEEL, wxEVT_KEY_UP, wxKeyEventHandler(wxKeyboard::OnKeyUp), NULL, this);
The events work fine, but every time I press a key I get an annoying "ding" from Windows. Probably because a slider doesn't natively understand keyboard events.

I've tried a few things to try to get rid of this, but nothing has worked.

The handlers are:

Code: Select all

void wxKeyboard::OnKeyDown( wxKeyEvent& event )
{
        int key = event.GetKeyCode();
	if( key == WXK_TAB )
	{
		return;
	}
        int note = GetNoteValue( key );
	if( note == 0 )
	{
		event.Skip(true);
		return;
	}
	// Only play if the note is not already playing -- prevents retriggers.
	if( !_playing[note] )
	{
		_releasing[note] = false;
		PlayNote( note );
	}
	event.Skip(true);
}

void wxKeyboard::OnKeyUp( wxKeyEvent& event )
{
	int key = event.GetKeyCode();
	int note = GetNoteValue( key );
	if( note == 0 )
	{
		event.Skip(true);
		return;
	}
	StopNote( note );
        event.Skip(true);
}
I've tried both 'true' and 'false' in event.Skip() and it hasn't mattered.

In this custom wxBitmapSlider control, I'm trying to prevent keyboard focus in the first place, but this doesn't seem to have any effect:

Code: Select all

	virtual bool AcceptsFocusFromKeyboard() const { return false; } 
	virtual bool AcceptsFocus() const { return false; }
I've also tried catching key down/up events within the custom control and propagating/skipping them there to no avail. The events work, and I still get that stupid ding.

Here's an earlier version of the sort of app I'm working on so you get the idea of what I'm trying to do:
Image

What I'm trying to accomplish is allowing someone to move one of the two sliders on the left (pitch and modulation controls) while still being able to use the letter keys to control what notes are playing.

Since the slider gets focus when someone clicks it, I have to be able to process keys, or prevent focus, or automatically move focus back to another control. None of the other controls on the dialog are a problem, but the slider is, and I suspect it's because sliders don't natively handle key events.

Any suggestions?
WinVista/7: VC++ .Net 2010 / Ubuntu 11.04: gcc4.4.3 [2.8.12 on all]

Stlawance
In need of some credit
In need of some credit
Posts: 5
Joined: Fri Jun 24, 2011 2:02 am

Re: wxSlider Processing Problem With wxKeyEvent (ding)

Post by Stlawance » Mon Jul 04, 2011 9:27 am

I also came across the same question. In the reference it says that EVT_SLIDER(id,func) can handle the events caused by wxEVT_COMMAND_SLIDER_UPDATED.It means when you move the button,this function can update the UI focus. But I don't know how to use it. Have you settled your problem? Can you give me some examples on how to update the slider status and pass the status to other part of the project.Thanks.

Xangis
Earned some good credits
Earned some good credits
Posts: 122
Joined: Fri Apr 14, 2006 9:49 pm
Location: Beaverton, OR
Contact:

Re: wxSlider Processing Problem With wxKeyEvent (ding)

Post by Xangis » Mon Jul 04, 2011 4:50 pm

I ended up creating a control that was not derived from slider, but rather wxControl. Since I already had code to handle OnPaint, all I really needed to do was add methods to handle values and key/mouse events. In my case it was a constructor, Set/Get min/max/value, and event handlers for enter, leave, click, key down, key up, mouse move, and mouse release.

Here's the event table, that should be enough of a hint to get you started if you want to write your own control:

Code: Select all

BEGIN_EVENT_TABLE(wxBitmapSlider, wxControl)
    EVT_PAINT(wxBitmapSlider::OnPaint)
    EVT_KEY_DOWN(wxBitmapSlider::OnKeyDown)
    EVT_KEY_UP(wxBitmapSlider::OnKeyUp)
    EVT_LEFT_UP(wxBitmapSlider::OnMouseRelease)
    EVT_MOTION(wxBitmapSlider::OnMouseMove)
    EVT_LEFT_DOWN(wxBitmapSlider::OnMouse)
    EVT_LEAVE_WINDOW(wxBitmapSlider::OnLeave)
    EVT_ENTER_WINDOW(wxBitmapSlider::OnEnter)
END_EVENT_TABLE()
WinVista/7: VC++ .Net 2010 / Ubuntu 11.04: gcc4.4.3 [2.8.12 on all]

User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2255
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania
Contact:

Re: wxSlider Processing Problem With wxKeyEvent (ding)

Post by evstevemd » Mon Jul 04, 2011 11:39 pm

May be you can post your control on code dump? It will be really useful to do that, if it does not offend you =D>
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
[Ubuntu 19.04/Windows 10 Pro/MacOS 10.13 - GCC/MinGW/Clang, CodeLite IDE]

Post Reply