שתף באמצעות


Sending Key To Minimized Window

Question

Tuesday, February 3, 2009 1:45 PM

I have no clue how to go about this |:
But anyway basically...I have a game I want to play and want to be able to press like the number "6" without bringing the window to front (So not SendKeys)

I have some code but I dont think I can get it worked as this is a DirectX game and not a normal app

Option Strict On 
Imports System.Runtime.InteropServices 
 
Public Class Form1 
    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr 
    Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd As IntPtr, ByVal hWndChildAfterA As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr 
    Public Declare Function SendMessageString Lib "user32" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal wMsg As IntPtr, ByVal wParam As IntPtr, ByVal lParam As String) As IntPtr 
 
    Private Overloads Shared Function SendMessage(ByVal hWnd As IntPtr, _ 
     ByVal msg As Integer, ByVal wp As IntPtr, ByVal lp As IntPtr) As IntPtr 
    End Function 
 
    Const WM_KEYUP As Integer = &H101 
    Const WM_KEYDOWN As Integer = &H100 
    Const VK_UP As Integer = &H26 
    Const VK_DOWN As Integer = &H28 
 
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
        Dim windowHwnd = FindWindow(Nothing, "Pacific - Sn1p3") 
        Dim editHwnd = FindWindowEx(windowHwnd, CType(0, IntPtr), "CLIENT", Nothing) 
 
        SendMessage(windowHwnd, WM_KEYUP, _ 
        CType(VK_UP, IntPtr), CType(&HC0510001, IntPtr)) 
    End Sub 
End Class 

All replies (3)

Thursday, February 5, 2009 10:07 AM ✅Answered

Hi SpInKsT4R,
You could try this sample which works fine on my computer using the sendkeys to fire the Spider Solitaire(window game),
If yo want to simulate a keyboard press event, I think you need to use the PostMessage rather than SendMessage.

The basic difference between SendMessage and PostMessage is that SendMessage sends a message to another window by calling that window’s procedure and waiting for it to return, whereas PostMessage posts a message in the message queue associated with the thread that created the specified window and returns immediately without waiting.   For detail on these two Windows APIs, we can refer to
 http://msdn.microsoft.com/en-us/magazine/cc301431.aspx.  

***PostMessage Function
*The PostMessage function places (posts) a message in the message queue associated with the thread that created the specified window and returns without waiting for the thread to process the message.

http://msdn.microsoft.com/en-us/library/ms644944(VS.85).aspx

Option Strict On  
Imports System.Runtime.InteropServices  
 
Public Class Form1  
    Declare Auto Function FindWindow Lib "USER32.DLL" ( _  
         ByVal lpClassName As String, _  
         ByVal lpWindowName As String) As IntPtr  
 
    ' Activate an application window.    
    Declare Auto Function SetForegroundWindow Lib "USER32.DLL" _  
        (ByVal hWnd As IntPtr) As Boolean  
 
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  
        ' Dim windowHwnd = FindWindow(Nothing, "Spider Solitaire")  
 
        Dim gameHandle As IntPtr = FindWindow(Nothing, "Spider Solitaire")  
        SetForegroundWindow(gameHandle)  
 
        SendKeys.SendWait("({LEFT})")  
        SendKeys.SendWait("({LEFT})")  
        SendKeys.SendWait("({LEFT})")  
        SendKeys.SendWait("({up})")  
        SendKeys.SendWait("({h})")  
        SendKeys.SendWait("% n")  
 
 
    End Sub  
End Class 

Best wishes
xingwei Hu Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Thursday, February 5, 2009 4:15 PM

Thanks for your reply
Yes you were right I need to use PostMessage to send a key to my window while its minimized without restoring it now I just need to work out how
Ive seen many working examples, but none with a DirectX game yet :(

At least I know its possible :)


Friday, February 6, 2009 3:13 AM

Hi SpInKsT4R

I think this is a question is relate with the development of the Directx game, Maybe you need to call the API of your Directx game to host the keyboard press event, This forum is for VB.NET questions only so that i think you could ask the question in XNA forum. there are some experts could give you better sugggestion and you will get better support. 

http://forums.xna.com/forums/

Best wishes
xingwei Hu


Please remember to mark the replies as answers if they help and unmark them if they provide no help.