Link to home
Start Free TrialLog in
Avatar of deathman5
deathman5

asked on

HotKeys

hey,
check this code, it never seem to work, what ever I click (ctrl-K or ctrl-F) the form will be maximized...
why?

[CODE]
Private Const MOD_ALT = &H1
Private Const MOD_CONTROL = &H2
Private Const MOD_SHIFT = &H4
Private Const PM_REMOVE = &H1
Private Const WM_HOTKEY = &H312
Private Type POINTAPI
    x As Long
    y As Long
End Type
Private Type Msg
    hWnd As Long
    Message As Long
    wParam As Long
    lParam As Long
    time As Long
    pt As POINTAPI
End Type
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 PeekMessage Lib "user32" Alias "PeekMessageA" _
  (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Private Declare Function WaitMessage Lib "user32" () As Long
Private bCancel As Boolean
Private Sub ProcessMessages()
    Dim Message As Msg
    'loop until bCancel is set to True
    Do While Not bCancel
        'wait for a message
        WaitMessage
        'check if it's a HOTKEY-message
        If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
            'minimize the form
            WindowState = vbMinimized
        End If
        'let the operating system process other events
        DoEvents
    Loop
End Sub
Private Sub ProcessMessage()
    Dim Message As Msg
    'loop until bCancel is set to True
    Do While Not bCancel
        'wait for a message
        WaitMessage
        'check if it's a HOTKEY-message
        If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
            'minimize the form
            WindowState = vbMaximized
        End If
        'let the operating system process other events
        DoEvents
    Loop
End Sub

Private Sub Form_Unload(Cancel As Integer)
    bCancel = True
    'unregister hotkey
    Call UnregisterHotKey(Me.hWnd, &HBFFF&)
End Sub

Private Sub Form_Load()
     Dim ret As Long
    bCancel = False
    'register the Ctrl-F hotkey
    ret = RegisterHotKey(Me.hWnd, &HBFFF&, MOD_CONTROL, vbKeyF)
    'show some information
    Me.AutoRedraw = True
    Me.Print "Press CTRL-F to minimize this form"
    'show the form and
    Show
    'process the Hotkey messages
    ProcessMessages

     Dim rett As Long
    bCancel = False
    'register the Ctrl-F hotkey
    rett = RegisterHotKey(Me.hWnd, &HBFFB&, MOD_CONTROL, vbKeyK)
    'show some information
    Me.AutoRedraw = True
    Me.Print "Press CTRL-K to max this form"
    'show the form and
    Show
    'process the Hotkey messages
    ProcessMessage
End Sub
[/CODE]
Avatar of zzzzzooc
zzzzzooc

You've got a huge mess going on in that "code".:-)

1.) In the  Form_Load() event, you're calling the  ProcessMessages() sub which is basically an eternal loop. Because of that, anything after  ProcessMessages() will not get processed in Form_Load() since it waits for the ProcessMessages() to finish. This means you'll never register the 2nd hot-key.

2.) The ProcessMessages() sub waits for a Window Message and checks if it's a Hot-Key window message (WM_HOTKEY). If it is, it will maximize the form (WindowState = vbMaximized). That method of peeking for the WM_HOTKEY message doesn't show you how  to check for the hot-key identifier to distinguish between additionally registered hot-keys. You can do it via the Message (Msg) type.

3.) You're registering hot-keys with manually specified IDs. If the ID is already in use, the function will fail. You should use GlobalAtomAdd/Delete to return unique long values to use.

4.) And other issues..


I suggest sub-classing the form associated with the hot-key for the WM_HOTKEY messages. The hot-key identifier will be passed in one of the parameters. Might be complicated for you to implement so I did a search:

https://www.experts-exchange.com/questions/20785060/KeyPress-problem.html
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
deathman5,

try the solution i provided in the url:
https://www.experts-exchange.com/questions/20738177/KEYPRESS-question.html

cheers,
srimanth.
Avatar of deathman5

ASKER

thx all of you, all the answers were good but the accepted one was easiest & does the job
thx alot Idle_Mind & all of you
:)
--> sorry but I cant split points <--