Skip to main content

Control Hotkeys

Image Credits VNAMedia

This weekend I plan to learn coding hotkeys and sending them to Winforms, basically for fun. I was testing the Control + N hotkeys and came up with some really useful text book material. This particular code was made to test things and worked great. I was happy and posted it here to ease the work for you.

Public Const MOD_CTRL As Integer = &H2 'Control key
    Public Const WM_HOTKEY As Integer = &H312

    <DllImport("User32.dll")> _
    Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr, _
                        ByVal id As Integer, ByVal fsModifiers As Integer, _
                        ByVal vk As Integer) As Integer
    End Function

    <DllImport("User32.dll")> _
    Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr, _
                        ByVal id As Integer) As Integer
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, _
                        ByVal e As System.EventArgs) Handles MyBase.Load
        RegisterHotKey(Me.Handle, 100, MOD_CTRL, Keys.N)
        'RegisterHotKey(Me.Handle, 200, MOD_CTRL, Keys.C)
    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_HOTKEY Then
            Dim id As IntPtr = m.WParam
            Select Case (id.ToString)
                Case "100"
                    MessageBox.Show("You pressed CTRL+N key combination")
                    '  Case "200"
                    'MessageBox.Show("You pressed ALT+C key combination")
            End Select
        End If
        MyBase.WndProc(m)
    End Sub

You should also check out these sites to know about more details.

http://www.xtremevbtalk.com/showthread.php?t=185548

http://social.msdn.microsoft.com/Forums/pl-PL/vblanguage/thread/3cc4da38-7846-4277-8dae-ef8fe4e66dc8

http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/c1a24688-d844-4adc-9d85-416a7158c6ba/

http://www.eggheadcafe.com/searchform.aspx?search=WM_SYSKEYDOWN

http://www.kirsbo.com/2010/how-to-add-global-hotkeys-to-applications-in-vb-net/

Comments

Popular posts from this blog

Affinity towards attachment

Of late I realize how attached I get to people, places and even things.  This is not just limited to stuff that I care about, but also mundane things that I don’t really generally much about either.  On most days, I am not even conscious about it. But when I am, I find it increasingly weird.  This hits different than hoarding though. Garbage is something I can easily get rid off. It is just that the definition of what I consider garbage is limited. More worryingly, I find accepting stuff (or God forbid, people) that I considered a treasure can now safely be put in the bin extremely difficult. This was always the case to an extent, it is not new. Just that at my age I just have come to this self realization on my own.  What do I need to do about it? I am not sure to be honest. On one hand, I agree, obsessively being attached to anything or anyone is not healthy. But, at the same time, Life still works, relatively okay. Of course, inanimate objects are better in this r...

Saraswati Puja and Valentine's Day Coincide

I used to hear this a lot - for Bengalis (Bengali Hindus), Saraswati Pujo is equivalent to the Valentine's Day . Girls would wear yellow sarees and braid their hair, while guys will be wearing yellow punjabees and white dhoti or pajama.  This time on Feb 14, 2024, they fell on the same day. Woke up pretty early in the morning, showered, wore the dhuti and uttiyo and did Saraswati Puja. Most of the mantras I have got by heart now. After the pushpanjali , I was done and we (my mother and I) could then break our fast with the Mahaprasad . Took a day off for the day, but still joined for a quick catchup call.  This is how she looks like. There is a bit of a history here. Usually, Hindus get the vigraha from the shop every year, worship and then do visarjan (immersion) in a river or pond. However, she is with us since I was in class IX. When I was in Standard X, my grandmother had died. As part of the souch (relatively inauspicious greiving period), we could not get a new vigra...

Getting back to making stuff

I was off for a while. I mean, I was not. I painted an old shoecase that now looks a gorgeous white cupboard. I fixed my bicycle.  But beyond this, I did not do much beyond what my work paid me to do. So here I am, about to touch a new year of my life - and I am itching to get back into the habit of writing again.  Not just writing stuff here, but also making applications or music.  In fact, after a very long time I feel like sketching again. I think I am going to sketch my wife. Or may be finish my ex wife's sketch. I dunno. But I am gonna get started with a pencil tomorrow. Thanks to chatgpt, I have restarted my application brainstorming and dusted my old Github repos.  I think this is going to be a good run this time.