How to emulate key press event?

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
Big_Lebowski
Knows some wx things
Knows some wx things
Posts: 49
Joined: Wed May 13, 2009 8:08 am

How to emulate key press event?

Post by Big_Lebowski » Fri Mar 08, 2019 7:12 pm

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?
OS: Xubuntu 18.04, wxWidgets-3.1.0

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

Re: How to emulate key press event?

Post by ONEEYEMAN » 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.

Big_Lebowski
Knows some wx things
Knows some wx things
Posts: 49
Joined: Wed May 13, 2009 8:08 am

Re: How to emulate key press event?

Post by Big_Lebowski » Fri Mar 08, 2019 7:58 pm

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.
Of course I did it.

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

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

Re: How to emulate key press event?

Post by doublemax » 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.
Use the source, Luke!

Big_Lebowski
Knows some wx things
Knows some wx things
Posts: 49
Joined: Wed May 13, 2009 8:08 am

Re: How to emulate key press event?

Post by Big_Lebowski » Fri Mar 08, 2019 8:05 pm

I can catch key event

Code: Select all

void MyFrame::OnCharHook( wxKeyEvent& event )
{
    int keycode = event.GetKeyCode();
    wxLogMessage("Keycode: %d", keycode);
    event.Skip();
}
it's ok
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

Big_Lebowski
Knows some wx things
Knows some wx things
Posts: 49
Joined: Wed May 13, 2009 8:08 am

Re: How to emulate key press event?

Post by Big_Lebowski » Fri Mar 08, 2019 8:09 pm

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 have seen that.

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

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

Re: How to emulate key press event?

Post by ONEEYEMAN » 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):

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 );
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.

Big_Lebowski
Knows some wx things
Knows some wx things
Posts: 49
Joined: Wed May 13, 2009 8:08 am

Re: How to emulate key press event?

Post by Big_Lebowski » Fri Mar 08, 2019 8:26 pm

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):

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 );
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.
Thanks.

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 ;
it's ok.

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);
it seems to me there is more cute solution.
OS: Xubuntu 18.04, wxWidgets-3.1.0

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

Re: How to emulate key press event?

Post by ONEEYEMAN » Fri Mar 08, 2019 8:35 pm

Hi,
Why you are don't like it? What's the problem with it?

Thank you.

Big_Lebowski
Knows some wx things
Knows some wx things
Posts: 49
Joined: Wed May 13, 2009 8:08 am

Re: How to emulate key press event?

Post by Big_Lebowski » Fri Mar 08, 2019 8:50 pm

ONEEYEMAN wrote:
Fri Mar 08, 2019 8:35 pm
Hi,
Why you are don't like it? What's the problem with it?

Thank you.
I have to write additional code to track precision and decimal point.

Thank you.
OS: Xubuntu 18.04, wxWidgets-3.1.0

Post Reply