How do I make the form send keystrokes such as Tab, Enter, Left Arrow etc to itself without interfering with other programs? If I use the SendKeys.Send method, it would send it to whatever window is opened. Is there a way to only work with the form?
Very quick random example:
Let’s say I have 5 textBoxs and I click the ‘Tab’ button 5 times. What would happen ?
It would change the focus from one textbox to another right?
- Can I make this happen automatically … like make it happen in the background and only to the form itself. So If I’m typing something on Notepad, I don’t want to receive those 5 tabs.
By the way I know I can use this code to change the fofucs, but remember its only an example.
TextBox1.Focus
I was really hoping for someone to direct me to the right place, however I’m making a progress on my own, but I’m looking for more help. All I need is to click the tab button indirectly (without me being active on the form,) and without the sendkeys method. Is it possible?
Here is my attempt, which I think is wrong
Private Declare Ansi Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Public Const WM_CHAR = &H102
Dim hwnd As Integer = FindWindow(vbNullString, "Form1")
Dim x As Integer = FindWindowEx(hwnd, 0, "WindowsForms10.EDIT.app.0.14fd2b5", vbNullString)
Dim keys As Keys
' send some keys
SendMessage(x, WM_CHAR, Keys.Tab, 0)
Why is it wrong?
Because instead of clicking ‘tab’ it sends tab to the textbox, so I’m still focused on the same textbox. It didn’t actually change focus to the next textbox.
Please if its possible help me, if not I will try more on my own.
Since this is inside your own app there is absolutely no reason to fake input, a notoriously delicate area.
Simply call the methods you need directly.
EDIT
Since you seem very keen on faked input, I’ll try to explain what is hard about it. First of all input is not sent synchronously to windows, instead it it posted asynchronously to the message queue of the thread which owns the input focus. When that thread pumps its messages it delivers the input messages to the appropriate window.
The main complication with input messages is that the system holds hidden keyboard state information with them that you cannot fake through
PostMessage(). For example, read the documentation ofGetKeyboardState(). If you want to fake a SHIFT+TAB key combination then you simply can’t do it withPostMessage()because that would not put the SHIFT in the keyboard state. It can only be done withSendInput().