הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Friday, December 19, 2008 7:16 PM
In Visual Basic 2008 Express Edition, how would I send keystrokes to a specific window. Right now, I just keep the window I want on top and use sendkeys.send("{enter}"). The window that I want to send keystrokes to is my applications window.
All replies (13)
Friday, December 19, 2008 10:14 PM ✅Answered | 2 votes
Hi santaclauseguy,
Create a Button1 and try this code:
Option Explicit On |
Imports System.Runtime.InteropServices |
Public Class Form1 |
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As UInteger, ByVal dwExtraInfo As UInteger) |
Const KEYEVENTF_KEYUP = &H2 |
Private Declare Function ShowWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nCmdShow As SHOW_WINDOW) As Boolean |
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal zero As IntPtr, ByVal lpWindowName As String) As IntPtr |
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As IntPtr) As Integer |
Private Declare Function GetForegroundWindow Lib "user32" () As Integer |
Private Enum SHOW_WINDOW As Integer |
SW_HIDE = 0 |
SW_SHOWNORMAL = 1 |
SW_NORMAL = 1 |
SW_SHOWMINIMIZED = 2 |
SW_SHOWMAXIMIZED = 3 |
SW_MAXIMIZE = 3 |
SW_SHOWNOACTIVATE = 4 |
SW_SHOW = 5 |
SW_MINIMIZE = 6 |
SW_SHOWMINNOACTIVE = 7 |
SW_SHOWNA = 8 |
SW_RESTORE = 9 |
SW_SHOWDEFAULT = 10 |
SW_FORCEMINIMIZE = 11 |
SW_MAX = 11 |
End Enum |
<DllImport("user32.dll", SetLastError:=True)> _ |
Friend Shared Function SendInput(ByVal cInputs As Int32, ByRef pInputs As INPUT, ByVal cbSize As Int32) As Int32 |
End Function |
<StructLayout(LayoutKind.Explicit, pack:=1, Size:=28)> _ |
Friend Structure INPUT |
<FieldOffset(0)> Public dwType As InputType |
<FieldOffset(4)> Public ki As KEYBDINPUT |
End Structure |
<StructLayout(LayoutKind.Sequential, pack:=1)> _ |
Friend Structure KEYBDINPUT |
Public wVk As Int16 |
Public wScan As Int16 |
Public dwFlags As KEYEVENTF |
Public time As Int32 |
Public dwExtraInfo As IntPtr |
End Structure |
Friend Enum InputType As Integer |
Keyboard = 1 |
End Enum |
<Flags()> _ |
Friend Enum KEYEVENTF As Integer |
EXTENDEDKEY = 1 |
KEYUP = 2 |
[UNICODE] = 4 |
SCANCODE = 8 |
End Enum |
Private Sub SendTheKeys(ByVal flags As KEYEVENTF, ByVal key As Keys) |
Dim input As New INPUT |
Dim ki As New KEYBDINPUT |
input.dwType = InputType.Keyboard |
input.ki = ki |
input.ki.wVk = Convert.ToInt16(key) |
input.ki.wScan = 0 |
input.ki.time = 0 |
input.ki.dwFlags = flags |
input.ki.dwExtraInfo = IntPtr.Zero |
Dim cbSize As Integer = Marshal.SizeOf(GetType(INPUT)) |
Dim result As Integer = SendInput(1, input, cbSize) |
If result = 0 Then Debug.WriteLine(Marshal.GetLastWin32Error) |
End Sub |
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click |
Dim targetwindow As IntPtr = FindWindow(Nothing, "Untitled - Notepad") |
If targetwindow = 0 Then |
MsgBox("Cannot find window") |
Exit Sub |
End If |
SetForegroundWindow(targetwindow) |
ShowWindow(targetwindow, SHOW_WINDOW.SW_NORMAL) |
ShowWindow(targetwindow, SHOW_WINDOW.SW_HIDE) |
keybd_event(Keys.Enter, 0, 0, 0) |
keybd_event(Keys.Enter, 0, KEYEVENTF_KEYUP, 0) |
Dim message As String = "asdf" |
Dim d As String = message.ToUpper |
For Each c As Char In d |
Dim key As UInteger = Convert.ToInt16(c) |
SendTheKeys(0, key) |
SendTheKeys(KEYEVENTF.KEYUP, key) |
Next |
ShowWindow(targetwindow, SHOW_WINDOW.SW_MINIMIZE) |
SetForegroundWindow(Me.Handle) |
End Sub |
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load |
Dim proc As Process = New Process |
proc.StartInfo.FileName = Environment.SystemDirectory & "\notepad.exe" |
proc.Start() |
proc.WaitForInputIdle() |
Dim window As IntPtr = FindWindow(Nothing, "Untitled - Notepad") |
ShowWindow(window, SHOW_WINDOW.SW_MINIMIZE) |
End Sub |
End Class |
Hope this is helpful.
Also i have a question. Do you want to send keys to a textbox of a specific window or just send keys to that window?
Bill
Sunday, December 21, 2008 10:26 PM ✅Answered
santaclauseguy said:
Bill,
Doesn't the window still need to be on top in your code? Is there a way to make it send the keys when the window is in the background? Also, I am sending the keys to a textbox. Does that mean I have to do something like textbox1.focus?
Hi santaclauseguy,
You don't need to send keys to write to a textbox of other window. You can send the text instantly to that textbox.
Take a look at my second post in this link:
http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/d4d9828d-4425-4bf4-980e-b31f1f191f7f
This code finds the textbox of the other window and sending to it the text you want.
If you don't understand something tell me.
Bill
Friday, December 19, 2008 8:39 PM | 2 votes
Hi santaclauseguy,
Here i have two codes for the same issue:
http://social.msdn.microsoft.com/Forums/en-US/vblanguage/thread/d4d9828d-4425-4bf4-980e-b31f1f191f7f
Bill
Friday, December 19, 2008 8:47 PM | 1 vote
Use this pattern to send keys in VB:
AppActivate(TitleOfWindowToSendKeysTo)
SendKeys.Send(KeysToSend)
Friday, December 19, 2008 8:56 PM
AppActivate works, but is there a way to send the keystrokes silently. I would like to be able to do other things on the computer as it sends the keystrokes.
Friday, December 19, 2008 9:52 PM
What kind of noise does sending keys using AppAcitvate make?
Friday, December 19, 2008 10:13 PM
Hello,
Sending keys makes the "Ho Ho Ho" noise :) .
Regards,
Adjutor
Saturday, December 20, 2008 4:00 AM
Bill,
Doesn't the window still need to be on top in your code? Is there a way to make it send the keys when the window is in the background? Also, I am sending the keys to a textbox. Does that mean I have to do something like textbox1.focus?
Sunday, December 21, 2008 11:05 PM
One last thing, if in the future of my programming I need to send the keys to something that is not a textbox like a webbrowser control or something else, is that possible without the window being on top?
Sunday, December 21, 2008 11:20 PM | 1 vote
Hi santaclauseguy,
In VB.NET I think that you can only send keys to the foreground window, but i am not sure.
Bill
Saturday, May 28, 2011 1:03 PM
Thank You John, this works great!
Saturday, May 28, 2011 1:42 PM
it is sending them silently. Or not ? I've just tried it.
Saturday, May 28, 2011 1:47 PM
This thread is two years old, so original viewers may not be tracking this thread any longer. Your question seems to be that you are using SendKeys to send some characters to a window. Do you know if the active control on the window to which you are sending keys is a textbox, or something that accepts keys? For example, if something other than a textbox has focus, your code may not appear to "type" anything.
--
Mike