Page 1 of 1

wxSlider Processing Problem With wxKeyEvent (ding)

Posted: Fri Jan 29, 2010 4:29 pm
by Xangis
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?

Re: wxSlider Processing Problem With wxKeyEvent (ding)

Posted: Mon Jul 04, 2011 9:27 am
by Stlawance
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.

Re: wxSlider Processing Problem With wxKeyEvent (ding)

Posted: Mon Jul 04, 2011 4:50 pm
by Xangis
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()

Re: wxSlider Processing Problem With wxKeyEvent (ding)

Posted: Mon Jul 04, 2011 11:39 pm
by evstevemd
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>