Page 1 of 1

How to emulate key press event?

Posted: Fri Mar 08, 2019 7:12 pm
by Big_Lebowski
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?

Re: How to emulate key press event?

Posted: Fri Mar 08, 2019 7:31 pm
by ONEEYEMAN
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.

Re: How to emulate key press event?

Posted: Fri Mar 08, 2019 7:58 pm
by Big_Lebowski
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?

Re: How to emulate key press event?

Posted: Fri Mar 08, 2019 8:02 pm
by doublemax
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.

Re: How to emulate key press event?

Posted: Fri Mar 08, 2019 8:05 pm
by Big_Lebowski
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.

Re: How to emulate key press event?

Posted: Fri Mar 08, 2019 8:09 pm
by Big_Lebowski
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.

Re: How to emulate key press event?

Posted: Fri Mar 08, 2019 8:11 pm
by ONEEYEMAN
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.

Re: How to emulate key press event?

Posted: Fri Mar 08, 2019 8:26 pm
by Big_Lebowski
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.

Re: How to emulate key press event?

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

Thank you.

Re: How to emulate key press event?

Posted: Fri Mar 08, 2019 8:50 pm
by Big_Lebowski
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.