Sharathms
November 5th, 1999, 11:10 AM
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
Aaron Young
November 5th, 1999, 09:05 PM
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
adyoung@win.bright.net
aarony@redwingsoftware.com
santulan
November 7th, 1999, 10:16 PM
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