Page 1 of 1

Systemwide Hotkeys

Posted: Sat Oct 25, 2014 2:09 pm
by Roberto Lopez
Hi All,

As I've commented in another post, I'm working in an utility that sits in system tray.

The purpose of this utility is to capture hotkeys (systemwide) and paste a specific code number according key pressed.

It must work with focused edit control in any application.

To implement this, the hotkeys must be trapped even when our HMG app, is not the active one (remember, we are waiting in the system tray).

This situation remembers me a problem at the start of MiniGUI library development.

The "problem" was that hotkeys were available systemwide :)

Then, to solve this, I've filtered the WM_HOTKEY message, to ignore hotkeys when our app is not active.

So, the only thing required to have (optional) systemwide hotkeys is to create a publc flag (living at _HMG_SYSDATA) and according to it, activate or deactivate WM_HOTKEY filter.

A very easy to accomplish wish :)

Thanks for your attention.

Systemwide Hotkeys

Posted: Sat Oct 25, 2014 2:48 pm
by Pablo César
Interesting Roberto ! :)

Re: Systemwide Hotkeys

Posted: Sat Oct 25, 2014 3:20 pm
by gfilatov
Roberto Lopez wrote:Hi All,
...

So, the only thing required to have (optional) systemwide hotkeys is to create a publc flag (living at _HMG_SYSDATA) and according to it, activate or deactivate WM_HOTKEY filter.

A very easy to accomplish wish :)
Hi Roberto,

Please take a look for the following code's snippet from the Minigui Ext source:

Code: Select all

...
   CASE nMsg == WM_HOTKEY

      // Process HotKeys

      i := AScan ( _HMG_aControlIds , wParam )

      IF i > 0
         IF _HMG_aControlType [i] == "HOTKEY"
            IF _HMG_aControlParentHandles [i] == GetActiveWindow() .OR. _HMG_GlobalHotkeys == .T.
               IF _DoControlEventProcedure ( _HMG_aControlProcedures [i] , i )
                  RETURN 0
               ENDIF
            ENDIF
         ENDIF
      ENDIF
...
May be it give an idea to HMG developers :?:

Re: Systemwide Hotkeys

Posted: Sat Oct 25, 2014 6:01 pm
by Roberto Lopez
gfilatov wrote: Hi Roberto,

Please take a look for the following code's snippet from the Minigui Ext source:
<...>
Yes, of course. It's very easy.

In fact the Windows API function 'RegisterHotKey' reference, clearly states that 'Defines a system-wide hot key': http://msdn.microsoft.com/en-us/library ... s.85).aspx.

But I've selected this API function, since is extremely easier to implement than other alternatives and (at that time) that was my main development strategy :)

Re: Systemwide Hotkeys

Posted: Sat Oct 25, 2014 9:04 pm
by srvet_claudio
Hi Roberto,
see this demo:

Code: Select all

#include "hmg.ch"

Function Main

   DEFINE WINDOW Form_1 ;
      AT 0,0 ;
      WIDTH 500 HEIGHT 300 ;
      TITLE 'Press ALT+F9' ;
      MAIN

      ON KEY ALT+F9 ACTION MsgInfo ("HotKey Action")

   END WINDOW


   #define WM_HOTKEY      0x0312
   CREATE EVENT PROCNAME AllHotKey() MSG WM_HOTKEY
   
   Form_1.Center()
   Form_1.Activate()
Return Nil


// If the WM_HOTKEY message was generated by a system-defined hot key, wParam will be one of the following values:
#define IDHOT_SNAPWINDOW  (-1)   // The "snap desktop" hot key was pressed
#define IDHOT_SNAPDESKTOP (-2)   // The "snap window" hot key was pressed

#define MOD_ALT       1
#define MOD_CONTROL   2
#define MOD_SHIFT     4
#define MOD_WIN       8


Function AllHotKey()
LOCAL cFormName := ""
LOCAL hWnd   := EventHWND()
LOCAL wParam := EventWPARAM()
LOCAL lParam := EventLPARAM()

   GetFormNameByHandle    ( hWnd, @cFormName )
   ID     := wParam
   VK     := HIWORD (lParam)
   MOD_VK := LOWORD (lParam)

   IF MOD_VK == MOD_ALT
      MOD_VK := "ALT"
   ELSEIF MOD_VK == MOD_CONTROL
      MOD_VK := "CONTROL"
   ELSEIF MOD_VK == MOD_SHIFT
      MOD_VK := "SHIFT"
   ELSEIF MOD_VK == MOD_WIN
      MOD_VK := "WIN"
   ENDIF

   MsgInfo ({ "HotKey --> ID: ",ID ,"   FormName: "+cFormName, "   Mod: ",MOD_VK ,"   VirtualKey: ",VK })

Return NIL

Re: Systemwide Hotkeys

Posted: Sun Oct 26, 2014 12:49 am
by Roberto Lopez
srvet_claudio wrote:Hi Roberto,
see this demo:
<...>
Nice!

I usually forget the CREATE EVENT command :)

Re: Systemwide Hotkeys

Posted: Mon Oct 27, 2014 6:03 pm
by Agil Abdullah
Interesting. I've no idea about it before reading this topic.

Many-many thanks.