Click here to Skip to main content
15,868,236 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I need to simulate the tab function in a on screen keyboard.

I will have 2-5 textboxes, I want The onscreen tab button to tab to each textbox in order.

I can write a function using select case, but just wondering if there is a simpler way.

I have looked at sendkeys, but it seem it is mainly used to sendkeys to other applications. can it be used for my needs and an example will be appreciated.
Posted
Comments
CHill60 30-Oct-15 11:02am    
If you tab to each textbox in order then you will always end up with the cursor in the last textbox. What are you actually trying to do?
Member 12088427 30-Oct-15 12:38pm    
There will be no keyboard to press tab on. The tab will be a button on the form, which I want to act like the keyboard tab button.

Using SendKeys for UI development is the sign of some abuse. As to the on-screen keyboard, it's simply useless, if you want to make your keyboard system-global. Many applications (notably, WPF) don't even windowed controls for input controls, they will ignore your emulated keyboard messages. The only valid way to use it would be via the P/Invoked Windows API SendInput:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms646310%28v=vs.85%29.aspx[^],
http://www.pinvoke.net/default.aspx/user32.sendinput[^].

Then you truly emulate a keyboard, as this function is low-level, acts as if you generated real hardware events, it is also very reliable and easy to use.

There is one related problem: your keyboard may steal input focus from a focused control, so you would not be able to input events, unless you take special measures to overcome this problem. For System.Windows.Forms, I described my solution here: Application focus getting and losing[^].

See also my past answers in related topics:
How to fire a Key Press event of a Key on button click in WPF[^],
Creating a Virtual Keyboard[^],
Programming on BACKSPACE button[^].

And this one is on WPF: How to fire a Key Press event of a Key on button click in WPF[^].

Good luck,
—SA
 
Share this answer
 
Comments
Member 12088427 30-Oct-15 12:37pm    
The on screen keyboard will only be for the textboxes within my own application. I have no intention of sending any keys outside the application.

Users will not have access to a physical keyboard, hence the need for the on screen keyboard.

As for the focus issue, I have overcome that using a variable, which holds the name of the textbox with focus. So I'll know which textbox the key are to go to.
Sergey Alexandrovich Kryukov 30-Oct-15 12:58pm    
Well, then you also don't need to use SendKeys. You are right about the "variable". Now when you have this reference, you should better handle the keys on the virtual keyboard and directly modify the value of the property TextBox.Text: insert a character, remove a character before cursor on backspace and after cursor on Del, and so on. This is way easier approach. You can also implement arrow keys, selection, add copy and past buttons. All for all, SendKeys is a bad thing, I would never ever use it.
—SA
Member 12088427 30-Oct-15 13:44pm    
Thanks. I have already created my own functions for Del, Backspace and Space. Will look at the code by Chill60. Might be what I wanted.
CHill60 30-Oct-15 13:31pm    
If you know the name of the "next" textbox then just use something like Dim x As TextBox = FindNext()
x.Focus()
in your button - where the FindNext function returns a TextBox and contains whatever your logic is for determining the next one
Member 12088427 30-Oct-15 13:45pm    
This is looking good, I'll have a look if I can use it for my needs. This would be much quicker than creating a case for each text box.
Think I have cracked it. I have added all of the textboxes in a array and send the name of the active text box as an argument to the Findnext function.

The function finds the next item in the array and returns focus to it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900