Hi Dw

I'm working on a program which has many forms and these forms are for setting the functionality of this program so now I want to detect if, let say Shift+F is pressed then the appropriate form will show. I don't want to use the Form_KeyPress or KeyDown or KeyUp because this form won't be visible to the screen so I want even if a user is let say opened a word processing program if the user presses these keys in combination the appropriate form will show up.

Thank you.

Recommended Answers

All 7 Replies

use the shortcut key for the menu item and use normal form.show() code in menu button.

Following your posts I guess you want to have a keylogger that does something when a keycombination is pressed. I would like to hate using a worprocessor and a keycombination and something unexpectedly pops up. This is just a guess on your other posts regarding catching keypress events, installing exe files without a user noticing etc. I hope your program never lands on my computer!

There are various hotkey programs availible. I use Qliner Hotkeys. You can google for alternatives.

Look if you feel you don't want to help please do no comment here please find another post to raise you post at not here, I said this program has multiple forms which the USER (maybe that wasn't clear to you) will use to set the functionality and activate and deactivate features so I don't know which part says I'm making a keylogger in this application if you wanted to know or perhaps steal my idea you should have asked not making everyone thing the bad side, this will be interacting with the user and the user will be the one who activate feature like Cloud backup by pressing these combination keys that I'm asking about here.
Well what I was looking for was Global Keypressed detection and I think I've found the solution.

Could you please post the solution you found? It may be of help to other users. For future reference, if you have a comment aimed at a specific user it is best to identify the target in your post to avoid confusion. A typical way is to post

@someuser - this is my comment directed to someuser

Here's a class I've made for doing global hooks(mouse and keyboard). Not really finished and not well commented.

Imports System.Runtime.InteropServices
Imports System.Reflection

Public Class cHook
#Region "Imports"
    'Mouse
    Private Declare Function SetMouseHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As MouseHookDelegate, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
    Private Declare Function CallNextMouseHookEx Lib "user32" (ByVal hHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As MouseHookStruct) As Integer
    Private Delegate Function MouseHookDelegate(ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As MouseHookStruct) As Integer
    Private mouseCallBack As MouseHookDelegate
    'Keyboard
    Private Declare Function SetKeyHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As KeyboardHookDelegate, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
    Private Declare Function CallNextKeyHookEx Lib "user32" (ByVal hHook As Integer, ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As KeyHookStruct) As Integer
    Private Delegate Function KeyboardHookDelegate(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KeyHookStruct) As Integer
    Private keyCallback As KeyboardHookDelegate
    'Shared
    Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Integer) As Integer
#End Region
#Region "Global Vars"
    'Constants
    Private Const WH_KEYBOARD_LL As Integer = 13&
    Private Const KEYEVENTF_KEYDOWN As Long = &H0
    Private Const KEYEVENTF_KEYUP As Long = &H2
    Private Const WH_MOUSE As Integer = 14
    'Variables
    Public KeyboardHandle As Integer
    Public MouseHandle As Integer
#End Region
#Region "Events"
    'Keyboard
    Public Event onKeyDown(ByVal vkCode As Integer, ByVal flags As Integer)
    Public Event onKeyUp(ByVal vkCode As Integer, ByVal flags As Integer)
    'Mouse
    Public Event onMouseDown(ByVal button As Windows.Forms.MouseButtons, ByVal location As Point)
    Public Event onMouseUp(ByVal button As Windows.Forms.MouseButtons, ByVal location As Point)
    Public Event onMouseMove(ByVal location As Point)
    Public Event onMouseScroll()
#End Region
#Region "Structures"
    'Keyboard
    Public Structure KeyHookStruct
        Public vkCode As Integer
        Public scanCode As Integer
        Public flags As Integer
        Public time As Integer
        Public dwExtraInfo As Integer
    End Structure
    'Mouse
    Public Structure MouseHookStruct
        Public pt As Point
        Public hwnd As Integer
        Public wHitTestCode As Integer
        Public dwExtraInfo As Integer
    End Structure
#End Region
#Region "Functions"
    'Keyboard
    Public Function HookKeyboard() As String
        keyCallback = AddressOf KeyboardCallbackFunction

        KeyboardHandle = SetKeyHookEx(WH_KEYBOARD_LL, keyCallback, Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly.GetModules()(0)).ToInt32, 0)
        If KeyboardHandle = 0 Then
            Return "Err:" & Err.Description
        Else
            Return ""
        End If
    End Function
    Public Function KeyboardCallbackFunction(ByVal Code As Integer, ByVal wParam As Integer, ByRef lParam As KeyHookStruct) As Integer
        If lParam.flags > 127 Then
            RaiseEvent onKeyUp(lParam.vkCode, lParam.flags - 128)
        Else
            RaiseEvent onKeyDown(lParam.vkCode, lParam.flags)
        End If
        Return CallNextKeyHookEx(KeyboardHandle, Code, wParam, lParam)
    End Function
    Public Sub UnhookKeyboard()
        Call UnhookWindowsHookEx(KeyboardHandle)
        KeyboardHandle = 0
    End Sub
    'Mouse
    Public Function MouseCallbackFunction(ByVal nCode As Integer, ByVal wParam As Integer, ByRef lParam As MouseHookStruct) As Integer
        If (nCode < 0) Then
            Return CallNextMouseHookEx(MouseHandle, nCode, wParam, lParam)
        End If
        Dim tmpText As String
        'MsgBox(wParam)
        Select Case wParam
            Case 512
                RaiseEvent onMouseMove(lParam.pt)
            Case 513
                RaiseEvent onMouseDown(MouseButtons.Left, lParam.pt)
            Case 514
                RaiseEvent onMouseUp(MouseButtons.Left, lParam.pt)
            Case 516
                RaiseEvent onMouseDown(MouseButtons.Right, lParam.pt)
            Case 517
                RaiseEvent onMouseUp(MouseButtons.Right, lParam.pt)
            Case 519
                RaiseEvent onMouseDown(MouseButtons.Middle, lParam.pt)
            Case 520
                RaiseEvent onMouseUp(MouseButtons.Middle, lParam.pt)
            Case 522
                RaiseEvent onMouseScroll()
            Case Else
                tmpText = wParam
        End Select
        Return CallNextMouseHookEx(MouseHandle, nCode, wParam, lParam)

    End Function
    Public Function HookMouse() As String
        mouseCallBack = AddressOf MouseCallbackFunction
        MouseHandle = SetMouseHookEx(WH_MOUSE, mouseCallBack, Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)), 0)
        If MouseHandle = 0 Then
            Return "Err:" & Err.Description
        Else
            Return ""
        End If
    End Function
    Public Sub UnhookMouse()
        UnhookWindowsHookEx(MouseHandle)
        MouseHandle = 0
    End Sub
#End Region
End Class

@me655321 thanks but I've already found a solution thanks again for your post.
@Reverend Jim the solution I've found is at: www.dreamincode.net/forums/topic/94406-global-hotkey/ but I was having this on my program but it was giving me problems when I was calling it but after reading this post I saw that I was calling it the wrong way and this showed the right way.

Thank you all with your positive posts this case has been closed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.