Click here to Skip to main content
15,887,683 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: Problem with System.Text.Encoding, Solved Pin
Eddy Vluggen4-Oct-18 2:15
professionalEddy Vluggen4-Oct-18 2:15 
QuestionCheck for changes on Windows form Pin
wjburke22-Oct-18 11:59
wjburke22-Oct-18 11:59 
AnswerRe: Check for changes on Windows form Pin
Eddy Vluggen2-Oct-18 21:49
professionalEddy Vluggen2-Oct-18 21:49 
SuggestionRe: Check for changes on Windows form Pin
Richard Deeming3-Oct-18 1:47
mveRichard Deeming3-Oct-18 1:47 
QuestionRe: Check for changes on Windows form Pin
dan!sh 3-Oct-18 2:09
professional dan!sh 3-Oct-18 2:09 
QuestionIndex and length must refer to a location within the string vb.net Pin
Victoryy Hisar Sitanggang2-Oct-18 4:14
Victoryy Hisar Sitanggang2-Oct-18 4:14 
AnswerRe: Index and length must refer to a location within the string vb.net Pin
Richard Deeming2-Oct-18 4:34
mveRichard Deeming2-Oct-18 4:34 
QuestionRegisterHotKey Pin
JR2122-Oct-18 1:58
JR2122-Oct-18 1:58 
Hi,

I use for some years this code that worked.
Public Class HotKey
        ' Windows API functions and constants
        Private Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
        Private Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
        Private Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString As String) As Short
        Private Declare Function GlobalDeleteAtom Lib "kernel32" (ByVal nAtom As Short) As Short

<pre>
    Public Const MOD_ALT As Integer = 1
    Public Const MOD_CONTROL As Integer = 2
    Public Const MOD_SHIFT As Integer = 4
    Public Const MOD_WIN As Integer = 8
    Private f As Form
    Public Enum EKeyModifier
        Neutral
        Alt
        Control
        Control_Alt
        Shift
        Shift_Alt
        Shift_Control
        Shift_Alt_Control
        Windows
        Win_Alt
        Win_Control
        Win_Control_Alt
        Win_Shift
        Win_Shift_Alt
        Win_Shift_Control
        Win_Shift_Alt_Control
        Norepeat = &H4000
    End Enum
    Private _Modifier As EKeyModifier
    Private _Key As System.Windows.Forms.Keys        ' the id for the hotkey
    Dim hotkeyID As Short
    Sub SDummy()
        MsgBox("Dummy routine ShortKey")
    End Sub
    Public Property Key() As System.Windows.Forms.Keys
        Get
            Return _Key
        End Get
        Set(ByVal value As System.Windows.Forms.Keys)
            UnregisterGlobalHotKey()
            RegisterGlobalHotKey(value, _Modifier, f)
            _Key = value
        End Set
    End Property
    Public Property Modifier() As EKeyModifier
        Get
            Return _Modifier
        End Get
        Set(ByVal value As EKeyModifier)
            UnregisterGlobalHotKey()
            RegisterGlobalHotKey(_Key, value, f)
            _Modifier = value
        End Set
    End Property

    ' register a global hot key
    Function RegisterGlobalHotKey(ByVal hotkey As EKeyModifier, ByVal modifiers As Integer, ByVal frm As Form) As Boolean
        Try
            f = frm
            ' use the GlobalAddAtom API to get a unique ID (as suggested by MSDN docs)
            'adapted code(obsolete)
            'Dim atomName As String = AppDomain.GetCurrentThreadId.ToString("X8") & Me.Name
            Dim th As New Threading.Thread(AddressOf SDummy)
            Dim value As Integer
            value = th.ManagedThreadId
            'end adapted
            Dim atomName As String = CStr(value) & "_HotKey"
            hotkeyID = GlobalAddAtom(atomName)
            If hotkeyID = 0 Then
                Throw New Exception("Unable to generate unique hotkey ID. Error code: " &
            System.Runtime.InteropServices.Marshal.GetLastWin32Error().ToString)
            End If
            _Key = hotkey
            _Modifier = modifiers
            ' register the hotkey, throw if any error

            If RegisterHotKey(frm.Handle, hotkeyID, modifiers, hotkey) = 0 Then
                Dim LastError As String = System.Runtime.InteropServices.Marshal.GetLastWin32Error.ToString
                Throw New Exception("Unable to register hotkey. Error code: " & LastError)
            End If
            Return True
        Catch ex As Exception
            ' clean up if hotkey registration failed
            UnregisterGlobalHotKey()
            Return False
        End Try
    End Function

    ' unregister a global hotkey
    Sub UnregisterGlobalHotKey()
        If hotkeyID <> 0 Then
            Try
                UnregisterHotKey(f.Handle, hotkeyID)
            Catch ex As Exception
            End Try
            ' clean up the atom list
            GlobalDeleteAtom(hotkeyID)
            hotkeyID = 0
        End If
    End Sub

    Protected Overrides Sub Finalize()
        UnregisterGlobalHotKey()
        MyBase.Finalize()
    End Sub
End Class</pre>

Now I want to use it again but my hotkey isn't working

I define the hotkey in the startup and check it in wndProc routine
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Hotkey = New Classes.HotKey
        If Not Hotkey.RegisterGlobalHotKey(Keys.F5, Classes.HotKey.EKeyModifier.Win_Alt, Me) Then
            MsgBox("Close key not registered")
        End If
    End Sub

<pre>
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    ' let the base class process the message
    MyBase.WndProc(m)

    ' if this is a WM_HOTKEY message, notify the parent object
    Const WM_HOTKEY As Integer = &H312
    If m.Msg = WM_HOTKEY Then
        ' do whatever you wish to do when the hotkey is pressed

        Dim key As Keys = ((m.LParam.ToInt64 >> 16) And &HFFFF) 'the charKey
        Dim modifier As Classes.HotKey.eKeyModifier = (m.LParam.ToInt64 And &HFFFF) 'Shift Alt Control and/or Windows key
        Select Case key.ToString
            Case "F5"
                EndToolStripMenuItem.PerformClick()
        End Select
    End If
End Sub</pre>

The WndProc function starts on other messages but not on the hotkey

Where am I wrong?

Thanks
Jan
AnswerRe: RegisterHotKey Pin
JR2126-Oct-18 12:53
JR2126-Oct-18 12:53 
QuestionProblems with SSLstream.Read (VB.NET/VS2017) Pin
Member 1185690428-Sep-18 21:58
Member 1185690428-Sep-18 21:58 
QuestionProblem with SHGetFolderPath(): Problem Closed. Pin
mo149228-Sep-18 11:30
mo149228-Sep-18 11:30 
AnswerRe: Problem with SHGetFolderPath(): Problem Closed. Pin
Richard MacCutchan28-Sep-18 21:45
mveRichard MacCutchan28-Sep-18 21:45 
GeneralRe: Problem with SHGetFolderPath(): Problem Closed. Pin
mo149228-Sep-18 23:44
mo149228-Sep-18 23:44 
SuggestionRe: Problem with SHGetFolderPath(): Problem Closed. Pin
Richard Deeming1-Oct-18 8:13
mveRichard Deeming1-Oct-18 8:13 
GeneralRe: Problem with SHGetFolderPath(): Problem Closed. Pin
mo14921-Oct-18 9:45
mo14921-Oct-18 9:45 
Questionhow to get corresponding values of particular column from database to text field Pin
Member 1400114828-Sep-18 5:45
Member 1400114828-Sep-18 5:45 
AnswerRe: how to get corresponding values of particular column from database to text field Pin
Richard MacCutchan28-Sep-18 6:14
mveRichard MacCutchan28-Sep-18 6:14 
AnswerRe: how to get corresponding values of particular column from database to text field Pin
Eddy Vluggen28-Sep-18 6:38
professionalEddy Vluggen28-Sep-18 6:38 
QuestionScreen size and form size Pin
desanti27-Sep-18 18:58
desanti27-Sep-18 18:58 
AnswerRe: Screen size and form size Pin
dan!sh 27-Sep-18 20:45
professional dan!sh 27-Sep-18 20:45 
AnswerRe: Screen size and form size Pin
Richard MacCutchan27-Sep-18 21:07
mveRichard MacCutchan27-Sep-18 21:07 
GeneralRe: Screen size and form size Pin
desanti28-Sep-18 8:59
desanti28-Sep-18 8:59 
GeneralRe: Screen size and form size Pin
Mycroft Holmes28-Sep-18 12:00
professionalMycroft Holmes28-Sep-18 12:00 
QuestionIs c++ worth learning? Pin
Member 1398247125-Sep-18 4:10
Member 1398247125-Sep-18 4:10 
AnswerRe: Is c++ worth learning? Pin
Richard MacCutchan25-Sep-18 5:41
mveRichard MacCutchan25-Sep-18 5:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.