CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Hotkey?

  1. #1
    Join Date
    Apr 1999
    Posts
    108

    Hotkey?

    Is there any code out there that will allow me to make hotkeys when the program is minimized... eg... when you press a, somethign happens, same when u press b etc..

    thanks

    - joe

    ?

  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: Hotkey?

    if you want to trap keyboard events even when your app is not active, you'll have to use windows hooks. Use the SetWindowsHookEx-API.


  3. #3
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Hotkey?

    Hi

    I was under the impression that you couldn't actually create a system -
    wide keyboard hook in VB. As I understand it, a VB Exe can only trap
    keyboard events in it's own process space.

    If you want to create a Keyboard hook, you'll need to use C/C++/Delphi or
    some other tool that can create DLL's. This DLL can then do a SendMessage
    to your VB application which you can capture with a subclassing routine.

    eg:


    #include "windows.h"
    #include "keyhook.h"
    #include "winuser.h"
    LRESULT CALLBACK KeyboardFunc (int nCode, WPARAM wParam, LPARAM lParam);
    HANDLE hInstance =0;
    HHOOK hhookKeyb =0;
    MSG Msg;
    BOOL WINAPI DllMain (HINSTANCE hinstDll, DWORD dwReason, LPVOID reserved)
    {
    hInstance = hinstDll;
    return TRUE;
    }
    LRESULT CALLBACK KeyboardFunc (int nCode, WPARAM wParam, LPARAM lParam)
    {
    LRESULT ret;
    /* Here we are in the Keyboard - Hook */
    if (nCode < 0)
    return (CallNextHookEx(hhookKeyb, nCode, wParam, lParam));
    else
    {
    ret = CallNextHookEx(hhookKeyb, nCode, wParam, lParam);

    if (wParam == 110) /* Which vKey is it??*/
    /* Now do something (maybe post a message or so */
    return 1;
    }
    else
    return ret;
    }
    }

    void InitKeyWatch (BOOL install)
    {
    if (install)
    {
    hhookKeyb = SetWindowsHookEx (WH_KEYBOARD, (HOOKPROC)KeyboardFunc, hInstance, 0);
    }
    else
    {
    UnhookWindowsHookEx(hhookKeyb);
    }
    }





    Regards

    Chris Eastwood


    CodeGuru - the website for developers
    http://www.codeguru.com/vb

  4. #4
    Join Date
    May 1999
    Posts
    3,332

    Re: Hotkey?

    Oops, you are right. I think a keyboard hook MUST reside in a DLL.
    Sorry for the confusion.


  5. #5
    Join Date
    Apr 1999
    Posts
    108

    Re: Hotkey?

    So i'd first install c++ (will borland do?) then paste that in and save it as a dll?
    Once it is saved, how do i use it(link it) in my vb app?

    ?

  6. #6
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Hotkey?

    Hi

    Borland C++ will be fine (in fact it's the only version of C++ I've ever used in anger).

    It's not as simple as doing a Cut+Paste though, the code I posted was very generalised and came from a FAQ I keep on my PC. You will still have to learn about Windows Hooks and how they work (and their limitations on capturing certain keystrokes).

    You'll also have to learn about creating Windows DLL's and how they're structured - I think the OpenFAQ on CodeGuru has an Article, if not, I'm sure I've got one lying around somewhere.

    - expect a LOT of crashes, especially where Windows Hooks are involved.


    Regards

    Chris Eastwood


    CodeGuru - the website for developers
    http://www.codeguru.com/vb

  7. #7
    Join Date
    Apr 1999
    Location
    Rotterdam, Netherlands
    Posts
    278

    Re: Hotkey? No need for a keyboard hook!

    Hi

    If you want that something happens in your program if you program doesn't have the focus, you can use a hotkey. (Like the Windowskey + E that will popup explorer, or they keys in ICQ to open the contactlist)
    It's quite easy, no need for C++.

    Place this in a module (and replace the Form1 with the formname of your own.
    Ctrl+F1 will set the caption to "Hotkey" and Ctrl+F2 clears it. Works fine in WinNT 4 SP4 and VB6.

    Option Explicit

    Private Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long, ByVal fsModifiers As Long, ByVal vk As Long) As Long
    Private Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long

    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

    Private Declare Sub MoveMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long)

    Private Const MOD_CONTROL = &H2
    Private Const VK_F1 = &H70
    Private Const VK_F2 = &H71

    Private Const WM_HOTKEY = &H312
    Private Const GWL_WNDPROC = (-4)

    Dim lOldWinProc As Long


    Public Sub Register()
    lOldWinProc = SetWindowLong(Form1.hwnd, GWL_WNDPROC, AddressOf SubClass1_WndMessage)
    Call RegisterHotKey(Form1.hwnd, 0, MOD_CONTROL, VK_F1)
    Call RegisterHotKey(Form1.hwnd, 1, MOD_CONTROL, VK_F2)
    End Sub

    Public Sub Unregister()
    Call UnregisterHotKey(Form1.hwnd, 0)
    Call UnregisterHotKey(Form1.hwnd, 1)
    Call SetWindowLong(Form1.hwnd, GWL_WNDPROC, lOldWinProc)
    End Sub


    Public Function SubClass1_WndMessage(ByVal hwnd As Long, ByVal Msg As Long, ByVal wp As Long, ByVal lp As Long) As Long
    If Msg = WM_HOTKEY Then
    If HIWORD(lp) = VK_F1 Then
    Form1.Caption = "HotKey"
    Else
    Form1.Caption = ""
    End If
    End If
    SubClass1_WndMessage = CallWindowProc(lOldWinProc, hwnd, Msg, wp, lp)
    End Function

    Public Function LOWORD(dwValue As Long) As Integer
    MoveMemory LOWORD, dwValue, 2
    End Function
    Public Function HIWORD(dwValue As Long) As Integer
    MoveMemory HIWORD, ByVal VarPtr(dwValue) + 2, 2
    End Function


    Hope this helps

    Crazy D


  8. #8
    Join Date
    Apr 1999
    Posts
    108

    Re: Hotkey? No need for a keyboard hook!

    Private Const MOD_CONTROL = &H2
    Private Const VK_F1 = &H70
    Private Const VK_F2 = &H71

    Private Const WM_HOTKEY = &H312


    what are the &h etc etc for?

    for me to set numerous hotkeys, what parts do i change?

    ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured