[Question] Send Keys to Window menu

User Tag List

Results 1 to 5 of 5
  1. #1
    TuFF's Avatar Contributor
    Reputation
    120
    Join Date
    Aug 2007
    Posts
    352
    Thanks G/R
    6/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Question] Send Keys to Window

    Alright Quick Question for VB6 Coders

    How do you use the SendKeys Function To A Specific window, Without having the window maxamized?

    What im trying to do: SendKeys to WoW While the game is minimized.

    Thanks!

    [Question] Send Keys to Window
  2. #2
    Jens's Avatar Contributor
    Reputation
    179
    Join Date
    Sep 2006
    Posts
    251
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you use PostMessage:
    Code:
     [DllImport("User32.Dll", EntryPoint = "PostMessageA")]
     static extern bool PostMessage(
         IntPtr hWnd,
         uint msg,
         int wParam,
         int lParam
    );
    
    const uint WM_KEYDOWN = 0x100;
    const uint WM_KEYUP = 0x101;
    const int WM_ARROWUP = 0x26;
    
    PostMessage(hWnd, WM_KEYDOWN, WM_ARROWUP, 0);
    PostMessage(hWnd, WM_KEYUP, WM_ARROWUP, 0);
    //will send the arrow up key! (more keys)

  3. #3
    Apoc's Avatar Angry Penguin
    Reputation
    1387
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/12
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    Imports System
    Imports System.Collections.Generic
    Imports System.Diagnostics
    Imports System.Runtime.InteropServices
    Imports System.Windows.Forms
    
    Class KeySender
        <DllImport("user32.dll", SetLastError := True)> _
        Private Shared Function FindWindowEx(ByVal hwndParent As IntPtr, ByVal hwndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
        End Function
        
        <DllImport("User32.Dll", EntryPoint := "PostMessageA")> _
        Private Shared Function PostMessage(ByVal hWnd As IntPtr, ByVal msg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As Boolean
        End Function
        
        <DllImport("user32.dll")> _
        Private Shared Function VkKeyScan(ByVal ch As Char) As Byte
        End Function
        
        Const WM_KEYDOWN As UInteger = 256
        
        Public Shared Function GetKey(ByVal character As Char) As Keys
            Return DirectCast(VkKeyScan(character), Keys)
        End Function
        
        Public Shared Sub SendMessageToWindow(ByVal windowHandle As IntPtr, ByVal message As IEnumerable(Of Char))
            For Each c As Char In message
                SendKeyToWindow(windowHandle, GetKey(c))
            Next
        End Sub
        
        Public Shared Sub SendMessageToWindow(ByVal process As Process, ByVal message As IEnumerable(Of Char))
            SendMessageToWindow(process.MainWindowHandle, message)
        End Sub
        
        Public Shared Sub SendMessageToWindow(ByVal windowName As String, ByVal message As IEnumerable(Of Char))
            For Each process As Process In Process.GetProcesses()
                If process.MainWindowTitle = windowName Then
                    SendMessageToWindow(process.MainWindowHandle, message)
                End If
            Next
        End Sub
        
        Public Shared Sub SendKeyToWindow(ByVal windowHandle As IntPtr, ByVal key As Keys)
            PostMessage(windowHandle, WM_KEYDOWN, CByte(key), 0)
        End Sub
        
        Public Shared Sub SendKeyToWindow(ByVal windowName As String, ByVal key As Keys)
            For Each process As Process In Process.GetProcesses()
                If process.MainWindowTitle = windowName Then
                    SendKeyToWindow(process.MainWindowHandle, key)
                    Exit For
                End If
            Next
        End Sub
        
        Public Shared Sub SendKeyToWindow(ByVal process As Process, ByVal key As Keys)
            SendKeyToWindow(process.MainWindowHandle, key)
        End Sub
    End Class
    That class will handle pretty much any key sending you need. (That's VB.NET)

    And in VB6 (using outdated API calls, since keybd_event is obsolete now, should look into SendInput instead) Taken from vbAccelerator - SendKeys using the API

    Code:
    Private Declare Sub keybd_event Lib "user32" ( _
       ByVal bVk As Byte, ByVal bScan As Byte, _
       ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    Private Const KEYEVENTF_EXTENDEDKEY = &H1
    Private Const KEYEVENTF_KEYUP = &H2
    
    Private Declare Function GetVersion Lib "kernel32" () As Long
    Private Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" ( _
       ByVal cChar As Byte) As Integer
    Private Declare Function VkKeyScanW Lib "user32" ( _
       ByVal cChar As Integer) As Integer
    
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
        lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)
    
    
    Public Sub KeyDown(ByVal vKey As KeyCodeConstants)
       keybd_event vKey, 0, KEYEVENTF_EXTENDEDKEY, 0
    End Sub
    
    Public Sub KeyUp(ByVal vKey As KeyCodeConstants)
       keybd_event vKey, 0, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0
    End Sub
    
    Public Function KeyCode(ByVal sChar As String) As KeyCodeConstants
    Dim bNt As Boolean
    Dim iKeyCode As Integer
    Dim b() As Byte
    Dim iKey As Integer
    Dim vKey As KeyCodeConstants
    Dim iShift As ShiftConstants
    
       ' Determine if we have Unicode support or not:
       bNt = ((GetVersion() And &H80000000) = 0)
       
       ' Get the keyboard scan code for the character:
       If (bNt) Then
          b = sChar
          CopyMemory iKey, b(0), 2
          iKeyCode = VkKeyScanW(iKey)
       Else
          b = StrConv(sChar, vbFromUnicode)
          iKeyCode = VkKeyScan(b(0))
       End If
       
       KeyCode = (iKeyCode And &HFF&)
    
    End Function
    Last edited by Apoc; 09-26-2008 at 01:36 AM.

  4. #4
    blupig's Avatar Member
    Reputation
    1
    Join Date
    Oct 2008
    Posts
    11
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or instead of having to go through all that complex code, why not just use the SendMessage API

  5. #5
    Apoc's Avatar Angry Penguin
    Reputation
    1387
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/12
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by blupig View Post
    Or instead of having to go through all that complex code, why not just use the SendMessage API
    Because SendMessage is slower, not as reliable, and you'd then have to handle message queues. The class I posted may be long, but it makes life easier since you can just call a method and not really worry about the actual win32 API.

Similar Threads

  1. [Question] Send money with paypal without a bank or VCC/CC
    By PublicEnemy219 in forum WoW Scams Help
    Replies: 2
    Last Post: 02-09-2009, 12:59 AM
  2. [Question]CD Keys/Gamecards
    By HolyGraal in forum World of Warcraft Gold Seller Reviews
    Replies: 4
    Last Post: 01-15-2009, 02:14 PM
  3. Sending Keys
    By EmiloZ in forum Programming
    Replies: 5
    Last Post: 10-12-2008, 05:40 AM
  4. Freebie method for sending keys to multiple WoW windows
    By tomit12 in forum World of Warcraft Guides
    Replies: 6
    Last Post: 03-03-2008, 10:49 AM
  5. Question: CD Keys
    By Yano in forum World of Warcraft General
    Replies: 8
    Last Post: 08-02-2006, 05:07 PM
All times are GMT -5. The time now is 09:43 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search