YeTi Posted December 14, 2005 Share Posted December 14, 2005 I'm having trouble getting a code that works for hotkeys. Kind of like a trainer. I am planning on running a game and having this program running in the background. I want to be able to press a certain key ingame and it do something to the game. I know what i want to do to the game. I am planning on using three hot keys. F1 F2 F3 Can anybody help me get a working code for this? R.I.P. Chi Shingi Meiyo 21/09/2005 - 07/03/2007 Andolini Mafia Family 16/08/2008 - Current Link to comment Share on other sites More sharing options...
Luke Posted December 14, 2005 Share Posted December 14, 2005 Heh, been there, done that. For hotkeys like you described, you need it to work globally, not just when your window has focus. For this you'll need a "low-level keyboard hook thing" (technical name). I've had hours of hair-ripping fun with callbacks in VB before, it's the thing that eventually pushed me over the edge and made me totally hate VB. For this though, you should just about be able to get it working. This might help you a bit adam broke it. Link to comment Share on other sites More sharing options...
Luke15 Posted December 15, 2005 Share Posted December 15, 2005 You can set up hot keys through the menu editor... Link to comment Share on other sites More sharing options...
Luke Posted December 15, 2005 Share Posted December 15, 2005 You can set up hot keys through the menu editor... They're not global... As in, system-wide, you can only use them if the window has focus. He said like a trainer. adam broke it. Link to comment Share on other sites More sharing options...
YeTi Posted December 16, 2005 Author Share Posted December 16, 2005 Thanks Luke's i managed to find out how to do it in the end. I've requested a lock for this. R.I.P. Chi Shingi Meiyo 21/09/2005 - 07/03/2007 Andolini Mafia Family 16/08/2008 - Current Link to comment Share on other sites More sharing options...
Toadyd Posted December 16, 2005 Share Posted December 16, 2005 As you wish Link to comment Share on other sites More sharing options...
Jevon Posted December 18, 2005 Share Posted December 18, 2005 Don't really see the point of locking it? Yeti, can you post your solution? Maybe someone else will need it in the future, and I imagine the method is the same for any language. "Face Your Fears, Live Your Dreams" - No Fear"God was a dream of good government." - Deus Ex Machina "I contend that we are both atheists. I just believe in one fewer god than you do. When you understand why you dismiss all the other possible gods, you will understand why I dismiss yours." - Stephen Roberts Link to comment Share on other sites More sharing options...
YeTi Posted December 19, 2005 Author Share Posted December 19, 2005 Yeti, can you post your solution? Ok but can everyone be clear from the begining i didn't write the original code i edited it for my needs. I found the code here Private Sub Form_Load() ' Register the hotkey. If RegisterHotKey(hWnd, HOTKEY_ID, _ MOD_ALT, VK_F10) = 0 _ Then MsgBox "Error registering hotkey." Unload Me End If ' Subclass the TextBox to watch for ' WM_HOTKEY messages. OldWindowProc = SetWindowLong( _ hWnd, GWL_WNDPROC, _ AddressOf NewWindowProc)End Sub' Look for the WM_HOTKEY message.Public Function NewWindowProc(ByVal hWnd As Long, ByVal Msg _ As Long, ByVal wParam As Long, ByVal lParam As Long) As _ LongConst WM_NCDESTROY = &H82Const WM_HOTKEY = &H312 ' If we're being destroyed, ' restore the original WindowProc and ' unregister the hotkey. If Msg = WM_NCDESTROY Then SetWindowLong _ hWnd, GWL_WNDPROC, _ OldWindowProc UnregisterHotKey hWnd, HOTKEY_ID End If ' See if this is the WM_HOTKEY message. If Msg = WM_HOTKEY Then Form1.Hotkey ' Process the message normally. NewWindowProc = CallWindowProc( _ OldWindowProc, hWnd, Msg, wParam, _ lParam)End Function' We got the hotkey.Public Sub Hotkey()Dim active_hwnd As LongDim wp As WINDOWPLACEMENT ' Get the active window's handle. active_hwnd = GetForegroundWindow() ' Get the window's current placement information. wp.length = Len(wp) GetWindowPlacement active_hwnd, wp ' Minimize it. wp.showCmd = SW_SHOWMINIMIZED ' Perform the action. SetWindowPlacement active_hwnd, wpEnd Sub That is the original code as you can see it's quite well commented. Anyway i wanted a code that would register a different hotkey so i changed it i was going to go with the F keys but i decided against it in the end and went with CTRL+X. So i changed the code to Private Sub Form_Load() ' Register the hotkey. If RegisterHotKey(hWnd, HOTKEY_ID, _ MOD_CONTROL, vbKeyX) = 0 _ Then MsgBox "Error registering hotkey." Unload Me End If ' Subclass the TextBox to watch for ' WM_HOTKEY messages. OldWindowProc = SetWindowLong( _ hWnd, GWL_WNDPROC, _ AddressOf NewWindowProc)End Sub' Look for the WM_HOTKEY message.Public Function NewWindowProc(ByVal hWnd As Long, ByVal Msg _ As Long, ByVal wParam As Long, ByVal lParam As Long) As _ LongConst WM_NCDESTROY = &H82Const WM_HOTKEY = &H312 ' If we're being destroyed, ' restore the original WindowProc and ' unregister the hotkey. If Msg = WM_NCDESTROY Then SetWindowLong _ hWnd, GWL_WNDPROC, _ OldWindowProc UnregisterHotKey hWnd, HOTKEY_ID End If ' See if this is the WM_HOTKEY message. If Msg = WM_HOTKEY Then Form1.Hotkey ' Process the message normally. NewWindowProc = CallWindowProc( _ OldWindowProc, hWnd, Msg, wParam, _ lParam)End Function' We got the hotkey.Public Sub Hotkey()Dim active_hwnd As LongDim wp As WINDOWPLACEMENT ' Get the active window's handle. active_hwnd = GetForegroundWindow() ' Get the window's current placement information. wp.length = Len(wp) GetWindowPlacement active_hwnd, wp ' Minimize it. wp.showCmd = SW_SHOWMINIMIZED ' Perform the action. SetWindowPlacement active_hwnd, wpEnd Sub Now instead of minimising the window i wanted to close a particular program. So i got this code to close minesweeper. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPrivate Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongConst WM_CLOSE = &H10Private Sub Form_Load()Dim winHwnd As LongDim RetVal As LongwinHwnd = FindWindow(vbNullString, "Minesweeper")If winHwnd <> 0 ThenPostMessage winHwnd, WM_CLOSE, 0&, 0&ElseMsgBox "Minesweeper is not open."End IfEnd Sub That will execute on form load but it can be placed in a loop, if statement of a command button. I placed my code into the hotkey code and ended up with this. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPrivate Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongConst WM_CLOSE = &H10Private Sub Form_Load() ' Register the hotkey. If RegisterHotKey(hWnd, HOTKEY_ID, _ MOD_CONTROL, vbKeyX) = 0 _ Then MsgBox "Error registering hotkey." Unload Me End If ' Subclass the TextBox to watch for ' WM_HOTKEY messages. OldWindowProc = SetWindowLong( _ hWnd, GWL_WNDPROC, _ AddressOf NewWindowProc)End Sub' Look for the WM_HOTKEY message.Public Function NewWindowProc(ByVal hWnd As Long, ByVal Msg _ As Long, ByVal wParam As Long, ByVal lParam As Long) As _ LongConst WM_NCDESTROY = &H82Const WM_HOTKEY = &H312 ' If we're being destroyed, ' restore the original WindowProc and ' unregister the hotkey. If Msg = WM_NCDESTROY Then SetWindowLong _ hWnd, GWL_WNDPROC, _ OldWindowProc UnregisterHotKey hWnd, HOTKEY_ID End If ' See if this is the WM_HOTKEY message. If Msg = WM_HOTKEY Then Form1.Hotkey ' Process the message normally. NewWindowProc = CallWindowProc( _ OldWindowProc, hWnd, Msg, wParam, _ lParam)End Function' We got the hotkey.Public Sub Hotkey()Dim winHwnd As LongDim RetVal As LongwinHwnd = FindWindow(vbNullString, "Minesweeper")If winHwnd <> 0 ThenPostMessage winHwnd, WM_CLOSE, 0&, 0&ElseMsgBox "Minesweeper is not open."End IfEnd Sub That particular code will load the form register the hotkey Control+X and listen for it. When Control+X is pressed it will check to see if there is a window running with the caption Minesweeper in it. If there is it will close it. If there isn't it will pop up a message box saying so. Please bear in mind i didn't personally write any of this code i did edit it a little though. R.I.P. Chi Shingi Meiyo 21/09/2005 - 07/03/2007 Andolini Mafia Family 16/08/2008 - Current Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now