CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 1999
    Location
    Bangalore, INDIA
    Posts
    70

    Shortcut Key on a form

    Hi,
    Can i trap Ctrl+F5 key on a form to do something like form minimize or call a function
    without using a menu.
    regards,
    Sharath


  2. #2
    Join Date
    Sep 1999
    Location
    Red Wing, MN USA
    Posts
    312

    Re: Shortcut Key on a form

    You could Register CTRL + F5 as a HotKey, then SubClass the Form to Capture the WM_HOTKEY Message allowing you to perform any function you want, eg.

    In a Module..

    public 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
    public Declare Function RegisterHotKey Lib "user32" (byval hwnd as Long, byval id as Long, byval fsModifiers as Long, byval vk as Long) as Long
    public Declare Function UnregisterHotKey Lib "user32" (byval hwnd as Long, byval id as Long) as Long

    public Const MOD_ALT = &H1
    public Const MOD_CONTROL = &H2
    public Const MOD_SHIFT = &H4
    public Const GWL_WNDPROC = (-4)
    private Const WM_HOTKEY = &H312

    public lPrevWnd as Long

    public Function SubWindow(byval hwnd as Long, byval Msg as Long, byval wParam as Long, byval lParam as Long) as Long
    If Msg = WM_HOTKEY And wParam = 1 then
    'Your HotKey's Been Triggered!
    Form1.WindowState = IIf(Form1.WindowState = vbMinimized, vbNormal, vbMinimized)
    End If
    SubWindow = CallWindowProc(lPrevWnd, hwnd, Msg, wParam, byval lParam)
    End Function



    In the Form..

    private Sub Form_Load()
    Call RegisterHotKey(hwnd, 1, MOD_CONTROL, vbKeyF5)
    lPrevWnd = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf SubWindow)
    End Sub

    private Sub Form_Unload(Cancel as Integer)
    Call UnregisterHotKey(hwnd, 1)
    'Always Close the Form Properly or VB will Crash!
    Call SetWindowLong(hwnd, GWL_WNDPROC, lPrevWnd)
    End Sub





    Aaron Young
    Analyst Programmer
    [email protected]
    [email protected]
    Aaron Young
    Senior Programmer Analyst (Red Wing Software)
    Certified AllExperts Expert

  3. #3
    Join Date
    Aug 1999
    Location
    India-Delhi
    Posts
    106

    Re: Shortcut Key on a form

    Hi,
    To trap a key on form follow the steps -
    1. In form property make keypreview = true.
    2. Use following code to trap any key on form. This is just an example to trap Ctrl-F5 key

    private Sub Form_KeyDown(KeyCode as Integer, Shift as Integer)
    Dim ShiftDown, AltDown, CtrlDown, Txt
    'Const vbKeyF2 = &H71 ' Define constants.
    Const vbShiftMask = 1
    Const vbCtrlMask = 2
    Const vbAltMask = 4
    ShiftDown = (Shift And vbShiftMask) > 0
    AltDown = (Shift And vbAltMask) > 0
    CtrlDown = (Shift And vbCtrlMask) > 0
    If KeyCode = vbKeyF5 And CtrlDown then ' Display key combinations.
    MsgBox ("You have pressed Crtl-F5")
    End If
    End Sub





    I hope this easy code solves your problems.

    Santulan

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