How to emulate key press event?
-
- Knows some wx things
- Posts: 49
- Joined: Wed May 13, 2009 8:08 am
How to emulate key press event?
Hello.
In my application, I have to make something like on-screen numeric keyboard.
Say, I press on wxButton with label "1" and it should emulate numeric key "1" press event.
I have seen this topic: viewtopic.php?f=1&t=27123
and this one: viewtopic.php?f=1&t=20228
but it's absolutely unclear, how to get this trick?
In my application, I have to make something like on-screen numeric keyboard.
Say, I press on wxButton with label "1" and it should emulate numeric key "1" press event.
I have seen this topic: viewtopic.php?f=1&t=27123
and this one: viewtopic.php?f=1&t=20228
but it's absolutely unclear, how to get this trick?
OS: Xubuntu 18.04, wxWidgets-3.1.0
Re: How to emulate key press event?
Hi,
You basically create a set of buttons for all keys on the keyboard.
Then you catch the wxEVT_BUTTON() for all those buttons.
And in the handler you call the appropriate method for an appropriate button.
You use event.GetId() or event.GetEventObject() to identify which button was clicked.
And when you construct appropriate panel you pass the parent frame to constructor in order to call the appropriate function.
Pretty simple.
Thank you.
You basically create a set of buttons for all keys on the keyboard.
Then you catch the wxEVT_BUTTON() for all those buttons.
And in the handler you call the appropriate method for an appropriate button.
You use event.GetId() or event.GetEventObject() to identify which button was clicked.
And when you construct appropriate panel you pass the parent frame to constructor in order to call the appropriate function.
Pretty simple.
Thank you.
-
- Knows some wx things
- Posts: 49
- Joined: Wed May 13, 2009 8:08 am
Re: How to emulate key press event?
Of course I did it.ONEEYEMAN wrote: ↑Fri Mar 08, 2019 7:31 pm Hi,
You basically create a set of buttons for all keys on the keyboard.
Then you catch the wxEVT_BUTTON() for all those buttons.
And in the handler you call the appropriate method for an appropriate button.
You use event.GetId() or event.GetEventObject() to identify which button was clicked.
And when you construct appropriate panel you pass the parent frame to constructor in order to call the appropriate function.
Pretty simple.
Thank you.
I created a set of wxButtons.
For every wxButton I added event function OnButton1...9Click(wxCommandEvent& event).
But how to emulate key "1" press event in OnButton1Click(wxCommandEvent& event) function?
OS: Xubuntu 18.04, wxWidgets-3.1.0
Re: How to emulate key press event?
Do you want to emulate key presses for other applications that yours? Then you need this:
https://docs.wxwidgets.org/trunk/classw ... lator.html
I don't know if it's implemented for all platforms.
https://docs.wxwidgets.org/trunk/classw ... lator.html
I don't know if it's implemented for all platforms.
Use the source, Luke!
-
- Knows some wx things
- Posts: 49
- Joined: Wed May 13, 2009 8:08 am
Re: How to emulate key press event?
I can catch key event
it's ok
But I don't know how and where to send keycode, to emulate key press event.
Code: Select all
void MyFrame::OnCharHook( wxKeyEvent& event )
{
int keycode = event.GetKeyCode();
wxLogMessage("Keycode: %d", keycode);
event.Skip();
}
But I don't know how and where to send keycode, to emulate key press event.
Last edited by Big_Lebowski on Fri Mar 08, 2019 8:09 pm, edited 1 time in total.
OS: Xubuntu 18.04, wxWidgets-3.1.0
-
- Knows some wx things
- Posts: 49
- Joined: Wed May 13, 2009 8:08 am
Re: How to emulate key press event?
I have seen that.doublemax wrote: ↑Fri Mar 08, 2019 8:02 pm Do you want to emulate key presses for other applications that yours? Then you need this:
https://docs.wxwidgets.org/trunk/classw ... lator.html
I don't know if it's implemented for all platforms.
I want to emulate key press in my application, say ... in my application there is a calculator, and I'd like to press any digit clicking by mouse.
OS: Xubuntu 18.04, wxWidgets-3.1.0
Re: How to emulate key press event?
Hi,
Why do you want to emulate anything?
Just execute the function you would normally execute when you use wxEVT_KEYDOWN().
Or you want to have "1" printed when you press the button with the label "1" in some text control?
Then all you do is this (pseudocode):
You don't need to call event.Skip() in this case.
I don't know if you want to handle "Backspace" and/or "Del", but you get the idea...
Thank you.
Why do you want to emulate anything?
Just execute the function you would normally execute when you use wxEVT_KEYDOWN().
Or you want to have "1" printed when you press the button with the label "1" in some text control?
Then all you do is this (pseudocode):
Code: Select all
wxString value = textCtrl->GetValue();
int pos = textCtrl->GetInsertionPos();
wxString newValue = value.substring( 0, pos ) + "1" + value.substr( ( pos + 1 ) );
textCtrl.SetValue( newValue );
I don't know if you want to handle "Backspace" and/or "Del", but you get the idea...
Thank you.
-
- Knows some wx things
- Posts: 49
- Joined: Wed May 13, 2009 8:08 am
Re: How to emulate key press event?
Thanks.ONEEYEMAN wrote: ↑Fri Mar 08, 2019 8:11 pm Hi,
Why do you want to emulate anything?
Just execute the function you would normally execute when you use wxEVT_KEYDOWN().
Or you want to have "1" printed when you press the button with the label "1" in some text control?
Then all you do is this (pseudocode):
You don't need to call event.Skip() in this case.Code: Select all
wxString value = textCtrl->GetValue(); int pos = textCtrl->GetInsertionPos(); wxString newValue = value.substring( 0, pos ) + "1" + value.substr( ( pos + 1 ) ); textCtrl.SetValue( newValue );
I don't know if you want to handle "Backspace" and/or "Del", but you get the idea...
Thank you.
My application has wxTextCtrl and touchscreen, it should be something like pos-terminal or bank-terminal...
There is no keyboard and mouse.
Now I print any digit for sample "1" in wxTextCtrl on keyboard:
Code: Select all
wxFloatingPointValidator<float> val(2, &m_float, wxNUM_VAL_ZERO_AS_BLANK);
m_textCtrl1->SetValidator(val);
.......
*m_textCtrl1 << 1 ;
But to print from my On-Screen keyboard, I have to use this ugly code:
Code: Select all
wxString m_digit;
......
m_digit += "2";
m_textCtrl1->SetValue(m_digit);
OS: Xubuntu 18.04, wxWidgets-3.1.0
Re: How to emulate key press event?
Hi,
Why you are don't like it? What's the problem with it?
Thank you.
Why you are don't like it? What's the problem with it?
Thank you.
-
- Knows some wx things
- Posts: 49
- Joined: Wed May 13, 2009 8:08 am
Re: How to emulate key press event?
I have to write additional code to track precision and decimal point.
Thank you.
OS: Xubuntu 18.04, wxWidgets-3.1.0