Jump to content
    1. Welcome to GTAForums!

    1. GTANet.com

    1. GTA Online

      1. Los Santos Drug Wars
      2. Updates
      3. Find Lobbies & Players
      4. Guides & Strategies
      5. Vehicles
      6. Content Creator
      7. Help & Support
    2. Red Dead Online

      1. Blood Money
      2. Frontier Pursuits
      3. Find Lobbies & Outlaws
      4. Help & Support
    3. Crews

    1. Grand Theft Auto Series

      1. Bugs*
      2. St. Andrews Cathedral
    2. GTA VI

    3. GTA V

      1. Guides & Strategies
      2. Help & Support
    4. GTA IV

      1. The Lost and Damned
      2. The Ballad of Gay Tony
      3. Guides & Strategies
      4. Help & Support
    5. GTA San Andreas

      1. Classic GTA SA
      2. Guides & Strategies
      3. Help & Support
    6. GTA Vice City

      1. Classic GTA VC
      2. Guides & Strategies
      3. Help & Support
    7. GTA III

      1. Classic GTA III
      2. Guides & Strategies
      3. Help & Support
    8. Portable Games

      1. GTA Chinatown Wars
      2. GTA Vice City Stories
      3. GTA Liberty City Stories
    9. Top-Down Games

      1. GTA Advance
      2. GTA 2
      3. GTA
    1. Red Dead Redemption 2

      1. PC
      2. Help & Support
    2. Red Dead Redemption

    1. GTA Mods

      1. GTA V
      2. GTA IV
      3. GTA III, VC & SA
      4. Tutorials
    2. Red Dead Mods

      1. Documentation
    3. Mod Showroom

      1. Scripts & Plugins
      2. Maps
      3. Total Conversions
      4. Vehicles
      5. Textures
      6. Characters
      7. Tools
      8. Other
      9. Workshop
    4. Featured Mods

      1. Design Your Own Mission
      2. OpenIV
      3. GTA: Underground
      4. GTA: Liberty City
      5. GTA: State of Liberty
    1. Rockstar Games

    2. Rockstar Collectors

    1. Off-Topic

      1. General Chat
      2. Gaming
      3. Technology
      4. Movies & TV
      5. Music
      6. Sports
      7. Vehicles
    2. Expression

      1. Graphics / Visual Arts
      2. GFX Requests & Tutorials
      3. Writers' Discussion
      4. Debates & Discussion
    1. Announcements

    2. Support

    3. Suggestions

Happy Holidays from the GTANet team!

Hot Keys In VB


YeTi
 Share

Recommended Posts

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?

user posted image
user posted image
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

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

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

Thanks Luke's i managed to find out how to do it in the end. I've requested a lock for this.

user posted image
user posted image
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

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

sig.jpg
Link to comment
Share on other sites

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.

user posted image
user posted image
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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.