How to realize a global reaction to pressing of some key independent of currently focused window (handling through all windows)?

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • How to realize a global reaction to pressing of some key independent of currently focused window (handling through all windows)?

      Hello.

      I write an application with many dialog windows, some of them have nested dialog windows etc.
      Every such window has the own callback function to react on some keys.

      Also this application should have a general menu, that should be called by pressing a dedicated hardware-key (for examble GUI_KEY_BACKSPACE) from everywhere, independent of currently opened window or its sub-window.

      One way is to add to every callback-function of correspondent window a code of reacting to this GUI_KEY_BACKSPACE. But it is a bad way.

      I would like to find out the other "generalized" way, where the handling of this MENU-key will be written only once at whole project.
      I have tried to set new callback-function for WM_HBKWIN, but it does not work, because this window is not in focus.

      How to do it?


      Does your Window Manager or whole emWin have an object (window or something else), which has a callback called always if any key is pressed?
      Or there is another way?

      Regards,
      Volodymyr.

      P.S. I already found the temporary solution, it uses GUI_GetKey() in main loop outside of Window Manager:

      C Source Code

      1. if (GUI_GetKey() == GUI_KEY_BACKSPACE && !menuIsActive) {
      2. menuIsActive = 1;
      3. prevFocusedWindow = WM_GetFocussedWindow();
      4. WM_ShowWindow(hMenuWindow);
      5. WM_BringToTop(hMenuWindow);
      6. WM_SetFocus(hMenuWindow);
      7. }
      8. // here is a part of menu callback-function, associated with menu window (hMenuWindow):
      9. switch (pMsg->MsgId) {
      10. case WM_KEY:
      11. if (pInfo->PressedCnt == 0) {
      12. switch (pInfo->Key) {
      13. case GUI_KEY_INSERT:
      14. WM_HideWindow(hMenuWindow);
      15. WM_ShowWindow(prevFocusedWindow);
      16. WM_BringToTop(prevFocusedWindow);
      17. WM_SetFocus(prevFocusedWindow);
      18. menuIsActive = 0;
      19. break;
      Display All
      But it is not the solution what I need because I did not find an appropriate solution how to catch "pressed" and "released" state of key using this function.

      If I call GUI_GetKeyState() after calling GUI_GetKey() or without GUI_GetKey() then it gives incorrect data (null as key and null as pressed state by first pressing of key and very unstable behaviour by next pressing):

      C Source Code

      1. if (GUI_GetKey() == GUI_KEY_BACKSPACE) {
      2. GUI_KEY_STATE keyState;
      3. GUI_GetKeyState(&keyState);
      4. // here the fields of keyState may be correct only after fourth or even more key pressing,
      5. // but on first key pressing they both are 0.
      6. if (keyState.Key == GUI_KEY_BACKSPACE && keyState.Pressed == 0 && !menuIsActive) {
      7. menuIsActive = 1;
      8. prevFocusedWindow = WM_GetFocussedWindow();
      9. WM_ShowWindow(hMenuWindow);
      10. WM_BringToTop(hMenuWindow);
      11. WM_SetFocus(hMenuWindow);
      12. }
      13. }
      Display All
      Maybe it works incorrect because I run it on the Windows Simulator.

      Also this way gives conflict if I use the same key for exit from menu in callback-function of menu itself - it exits from menu and goes to menu again.
      Therefore, as you can see in the first code sample, I use different keys, but I would like to use the same key for entry to menu and exit from it.

      Could you send a sample of correct and safe way using above mentioned functions of keyboard.
      Best regards,
      Volodymyr.

      The post was edited 4 times, last by volodymyr ().