Results 1 to 3 of 3
  1. #1
    MugNuf's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Posts
    790
    Reputation
    9
    Thanks
    160
    My Mood
    Goofy

    RegisterHotKey/UnregisterHotKey explanation?

    I've googled how to make hotkeys (I do not want to use the "ADD A TIMER AND PASTE DIS CODE" tutorials >_>), and came across this: FAQ: How do I register a hotkey in VB.NET?

    It offers no explanation on what anything does (although, I'm kinda sure what some things do but some of it just baffles me). Anyone want to explain it ?

  2. #2
    Jason's Avatar
    Join Date
    Apr 2010
    Gender
    male
    Location
    /dev/null
    Posts
    5,704
    Reputation
    918
    Thanks
    7,676
    My Mood
    Mellow
    Use the KeyboardHook in the MPGH SDK, then build a class around it to manage your hotkeys.

    EDIT: Okay I wrote it for you
    [highlight=vb.net]
    Imports MPGH
    Public Class HotkeyManager : Implements IDisposable
    Public Delegate Sub HotkeyAction(ByVal params As Object())

    Private Basehook As SDK.KeyboardHook
    Private Hotkeys As Dictionary(Of Integer, Hotkey)
    Private _isactive As Boolean

    Public ReadOnly Property IsActive As Boolean
    Get
    Return Me._isactive
    End Get
    End Property

    Public Sub New()
    Me.Hotkeys = New Dictionary(Of Integer, Hotkey)()
    Me.Basehook = New SDK.KeyboardHook(New SDK.KeyboardHook.OnKeyPressed(AddressOf OnKeyPressed))
    Me._isactive = Me.Basehook.IsHooked
    End Sub

    Private Sub OnKeyPressed(ByVal nKey As Integer)
    For Each k As Integer In Me.Hotkeys.Keys
    If nKey = k Then
    Dim curHotkey As Hotkey = Me.Hotkeys(k)
    curHotkey.Callback.Invoke(curHotkey.FunctionParams )
    Exit For
    End If
    Next
    End Sub
    Public Function RegisterHotkey(ByVal key As Integer, ByVal action As HotkeyAction, Optional ByVal params As Object() = Nothing) As Boolean
    If Not Me.Hotkeys.ContainsKey(key) AndAlso Not IsNothing(action) Then
    Me.Hotkeys.Add(key, New Hotkey(key, action, params))
    Return True
    Else
    Return False
    End If
    End Function

    Public Function UnregisterHotkey(ByVal key As Integer) As Boolean
    If Me.Hotkeys.ContainsKey(key) Then
    Me.Hotkeys.Remove(key)
    Return True
    Else
    Return False
    End If
    End Function

    Public Structure Hotkey
    Public ReadOnly Key As Integer
    Public ReadOnly Callback As HotkeyAction
    Public ReadOnly FunctionParams As Object()

    Public Sub New(ByVal key As Integer, ByVal callback As HotkeyAction, ByVal params As Object())
    Me.Key = key
    Me.Callback = callback
    Me.FunctionParams = params
    End Sub
    End Structure

    #Region "IDisposable Support"
    Private disposedValue As Boolean ' To detect redundant calls
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
    If Not Me.disposedValue Then
    If disposing Then
    Me.Basehook.Dispose()
    Me._isactive = Me.Basehook.IsHooked
    Me.Hotkeys = Nothing
    Me.Basehook = Nothing
    End If
    End If
    Me.disposedValue = True
    End Sub

    Public Sub Dispose() Implements IDisposable.Dispose
    Dispose(True)
    GC.SuppressFinalize(Me)
    End Sub
    #End Region

    End Class
    [/highlight]

    Now, this is really easy to use, add the class to your project then create a new global variable to hold a HotkeyManager instance, after that you simply have to write out some method handlers (as you would for buttons, etc) that match the delegate signature

    which is just a sub, which accepts an object() as a parameter.

    So, let's have a look at an example:

    [highlight=vb.net]
    Public Class Form1
    Private hKeyMgr As New HotkeyManager() 'initialize a hotkeymanager

    'when I click the button, it will register the hotkeys "D" and "A". Whenever D is pressed, the "OnDPressed" event will fire. Whenever "A" is pressed, the OnAPressed event will fire.
    Private Sub btnRegisterHotkey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    hKeyMgr.RegisterHotkey(Keys.D, AddressOf OnDPressed, New Object() {"hello", "world", "cool"}) '3 params to be passed to the function.
    hKeyMgr.RegisterHotkey(Keys.A, AddressOf OnAPressed) 'no params to be used.
    End Sub

    Private Sub OnDPressed(ByVal params As Object())
    Me.Text = String.Format("D was pressed with params: {0} - {1} - {2}", params) 'show that the parameters we wanted were passed successfully.
    End Sub

    Private Sub OnAPressed(ByVal params As Object())
    Me.Text = "A was pressed, GG." 'we didn't pass any params to this function, so we just ignore them.
    End Sub
    [/highlight]

    Okay, so that's pretty straightforward really, right? Just don't forget to Dispose the hotkeymanager class when you're done:

    [highlight=vb.net]
    If Me.hKeyMgr.IsActive Then Me.hKeyMgr.Dispose()
    [/highlight]
    Last edited by Jason; 09-13-2011 at 07:59 PM.

    Quote Originally Posted by Jeremy S. Anderson
    There are only two things to come out of Berkley, Unix and LSD,
    and I don’t think this is a coincidence
    You can win the rat race,
    But you're still nothing but a fucking RAT.


    ++Latest Projects++
    [Open Source] Injection Library
    Simple PE Cipher
    FilthyHooker - Simple Hooking Class
    CLR Injector - Inject .NET dlls with ease
    Simple Injection - An in-depth look
    MPGH's .NET SDK
    eJect - Simple Injector
    Basic PE Explorer (BETA)

  3. #3
    moebeeus's Avatar
    Join Date
    Sep 2011
    Gender
    male
    Posts
    6
    Reputation
    10
    Thanks
    83
    My Mood
    Mellow
    keyboard hooks can be dangerous to yr system, and registerhotkey sometimes(depending on the game) wont work when in fullscreen.
    getasynckeystate(used in a timer) is a good alternative, simple code for a small program like a trainer.
    if you decide to use getasynckeystate, i'll give u some crafty code for selectable hotkeys

Similar Threads

  1. all the words like wtf and there explanation
    By Pepijntje in forum General
    Replies: 24
    Last Post: 12-07-2021, 05:57 PM
  2. Explanation
    By Noxen in forum Combat Arms Hacks & Cheats
    Replies: 2
    Last Post: 08-06-2008, 05:19 PM
  3. The most random explanation ever...
    By gbitz in forum General
    Replies: 15
    Last Post: 07-30-2008, 11:57 PM
  4. Give me an explanation
    By Someguytwo in forum Suggestions, Requests & General Help
    Replies: 6
    Last Post: 01-19-2008, 10:17 PM
  5. Replies: 4
    Last Post: 06-06-2006, 10:16 PM