Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Linux x11 keyboard hook
Forum Updated to NodeBB v4.3 + New Features

Linux x11 keyboard hook

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • RobotechnicR Offline
    RobotechnicR Offline
    Robotechnic
    wrote on last edited by
    #1

    Hello,
    I want to create an application that can capture keyboard in the application until the user press a certain keystroke as VM do.
    I did that on windows with windows.h but now, I am on linux and my program don't work (as expected because I have used windows.h).
    My problem is that when I try to include X11 in my project I have a lots of errors (556):
    https://pastebin.com/1d7G8r5f
    Do you know how to do that?
    I am on arch linux with kde and x11

    Thanks in advance for your help.

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Robotechnic said in Linux x11 keyboard hook:

      Do you know how to do that?

      The X11 headers do some weird defines. Include them after the Qt includes or undefine them before including Qt headers.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • RobotechnicR Offline
        RobotechnicR Offline
        Robotechnic
        wrote on last edited by
        #3

        Thanks @Christian-Ehrlicher , now the code work bu only in main:

        int main(int argc, char *argv[])
        {
            bool quit = false;
            Display *d = XOpenDisplay(NULL);
        
            XGrabKeyboard(d, DefaultRootWindow(d),
                             true, GrabModeAsync, GrabModeAsync, CurrentTime);
        
           while(!quit) {
              XEvent ev;
        
              XNextEvent(d, &ev);
        
              switch (ev.type) {
              case KeyPress:
                char *s;
                unsigned int kc;
        
                kc = ((XKeyPressedEvent*)&ev)->keycode;
                s = XKeysymToString(XKeycodeToKeysym(d, kc, 0));
        
                if(s) {
                    qDebug()<<"Key :"<<s;
                }
                if(!strcmp(s, "q")) quit=~0;
              }
           }
        
           XUngrabKeyboard(d, CurrentTime);
        
           if (XCloseDisplay(d)) {
              return 1;
           }
        
            return 0;
        }
        

        When I try to put in in QMainWindows errors happen

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Will not work the way you do it - you block the Qt event loop.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • RobotechnicR Offline
            RobotechnicR Offline
            Robotechnic
            wrote on last edited by
            #5

            Ok, so how can I do it?

            1 Reply Last reply
            0
            • RobotechnicR Offline
              RobotechnicR Offline
              Robotechnic
              wrote on last edited by
              #6

              Sorry my question is not for the Qt main loop, I can do that with thread or QTimer this is not my problem.
              My problem was that when I have putted x11 headers in .h file, that generated errors but I fixed it and It work. Thanks @Christian-Ehrlicher , I have writing before thinking.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved