Im building a form that will function like a keypad on a touch screen. The problem Im having at the moment is that when I person presses a button for example the ‘1’ button, it must add that char ‘1’ to an edit box, which displays the keys already pressed. Now the problem Im experiencing is that once a person presses a key, I can add the char to the string already displayed in the edit box, but the cursor goes to the front of the edit box, and does not appear at the back. I use the following code to add a char to the edit box:
edtPassword.text := edtPassword.text + key;
Now that just adds the char to the end of the edit box, but how do I move the cursor to the end of the edit box.
Plus I do also have a backspace button, what code would I use the erase the last char of the string in the edit box if you clicked it?
Im using Delphi XE2
Disclaimer:
I’m not answering the question as it is. I’m trying to propose a way I’ll rather follow when I’d need a virtual keyboard.
1. What about ready made component ?
I would suggest you to use the
TTouchKeyboardcomponent, which is a VCL component, representing a virtual keyboard. That said, you’re developing something, what is already a part of Delphi distributions. It’s a part of Delphi since version 2010, but I can’t say in which distributions.2. It looks ugly and I’ll rather make my own:
When I’ve seen
TTouchKeyboardcomponent for the first time, I was hoping that allows owner drawing. Well, unfortunately doesn’t. In that case I’d try to simulate key strokes by myself rather than solve cases like this for another components you may soon or later use.2.1. How to simulate keystrokes in your own way ?
The following code simulates keystrokes by using the
SendInputfunction and it’s based on the code used byTTouchKeyboardcomponent:And the usage of the above function. You can pass virtual key, scan code or both to this function. When you’ll be unsure with either of them, pass value of -1 and the key code will be additionally mapped by the
MapVirtualKeyfunction. The following example shows how to send a Backspace and then Shift + A:2.2. How to simulate keystrokes in a forbidden way ?
You may also go against the reference and use the
SendKeyfromVcl.Touch.Keyboardunit. In the reference is stated that theSendKeyis used internally and shouldn’t be called but it’s visible outside of the unit and if you’re bold enough you can use it like this:2.3. How to simulate keystrokes in a different view ?
How to send text to another application ?How to send keys to another application ?How to send keys without using SendMessage and PostMessage ?How to programmatically simulate user input ?