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. always get key inputs from application (if app even not active)
Forum Updated to NodeBB v4.3 + New Features

always get key inputs from application (if app even not active)

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 328 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.
  • J Offline
    J Offline
    JacobNovitsky
    wrote on 10 Aug 2023, 12:44 last edited by
    #1

    Having some app, made with x11 libs, tested for ubuntu. It takes simple keyboard inputs and outputs corresponding value. It works fine if application window (ubuntu terminal) is currently open

    linux-kbhit.h
    
    #define     ESC     "\033"
    #define     UP      "\033[A"
    #define     DOWN    "\033[B"
    #define     LEFT    "\033[D"
    #define     RIGHT   "\033[C"
    #define     A   "a"
    
    void term_setup(void (*sighandler)(int));
    void term_restore();
    bool kbhit();
    bool keydown(const char* key);
    
    linux-kbhit.c
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #include <unistd.h>
    #include <poll.h>
    #include <signal.h>
    #include <termios.h>
    #include <sys/ioctl.h>
    
    static struct termios oldtio;
    static struct termios curtio;
    
    void term_setup(void (*sighandler)(int)){
        struct sigaction sa;
    
        /* Save stdin terminal attributes */
        tcgetattr(0, &oldtio);
    
        if(sighandler){
        /* Make sure we exit cleanly */
            memset(&sa, 0, sizeof(struct sigaction));
            sa.sa_handler = sighandler;
            sigaction(SIGINT, &sa, NULL);
            sigaction(SIGQUIT, &sa, NULL);
            sigaction(SIGTERM, &sa, NULL);
        }
    
        /* This is needed to be able to tcsetattr() after a hangup (Ctrl-C)
         * see tcsetattr() on POSIX
         */
        memset(&sa, 0, sizeof(struct sigaction));
        sa.sa_handler = SIG_IGN;
        sigaction(SIGTTOU, &sa, NULL);
    
        /* Set non-canonical no-echo for stdin */
        tcgetattr(0, &curtio);
        curtio.c_lflag &= ~(ICANON | ECHO);
        tcsetattr(0, TCSANOW, &curtio);
    }
    
    void term_restore(){
        /* restore terminal attributes */
        tcsetattr(0, TCSANOW, &oldtio);
    }
    
    static char get[4]= {0};
    bool kbhit(){
        struct pollfd pfds[1];
        int ret;
        memset(get, 0, sizeof(char) * 4);
    
        /* See if there is data available */
        pfds[0].fd = 0;
        pfds[0].events = POLLIN;
        ret = poll(pfds, 1, 0);
    
        /* Consume data */
        if (ret > 0) {
            read(0, get, 3);
            return strlen(get) > 0;
        }
        return false;
    }
    
    bool keydown(const char* key){
        return !strcmp(get, key);
    }
    
    it works perfect for keyboard inputs for active application, but application purpose is to get all OS key inputs and interpret it for further use
    
    main.c
    
    #include "linux-kbhit.h"
    #include "stdio.h"
    #include <signal.h>
    
    static sig_atomic_t end = 0;
    static void sighandler(int signo)
    {
        end = 1;
        printf("good beye!\n");
    }
    
    int main()
    {
        term_setup(sighandler);
    
        while (!end) {
            if (kbhit()) {
                if (keydown(ESC))
                    printf("This is \"ESC\" button!\n");
                if (keydown(UP))
                    printf("This is \"UP\" button!\n");
                if (keydown(DOWN))
                    printf("This is \"DOWN\" button!\n");
                if (keydown(LEFT))
                    printf("This is \"LEFT\" button!\n");
                if (keydown(RIGHT))
                    printf("This is \"RIGHT\" button!\n");
                if (keydown(A))
                    printf("This is \"A\" button!\n");
            }
        }
    
        term_restore();
    
        return 0;
    }
    

    Is there a way to get something similar to /dev/input (which is helpful) in Qt Library?
    https://thehackerdiary.wordpress.com/2017/04/21/exploring-devinput-1/

    C J 2 Replies Last reply 10 Aug 2023, 13:00
    0
    • J JacobNovitsky
      10 Aug 2023, 12:44

      Having some app, made with x11 libs, tested for ubuntu. It takes simple keyboard inputs and outputs corresponding value. It works fine if application window (ubuntu terminal) is currently open

      linux-kbhit.h
      
      #define     ESC     "\033"
      #define     UP      "\033[A"
      #define     DOWN    "\033[B"
      #define     LEFT    "\033[D"
      #define     RIGHT   "\033[C"
      #define     A   "a"
      
      void term_setup(void (*sighandler)(int));
      void term_restore();
      bool kbhit();
      bool keydown(const char* key);
      
      linux-kbhit.c
      
      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      
      #include <unistd.h>
      #include <poll.h>
      #include <signal.h>
      #include <termios.h>
      #include <sys/ioctl.h>
      
      static struct termios oldtio;
      static struct termios curtio;
      
      void term_setup(void (*sighandler)(int)){
          struct sigaction sa;
      
          /* Save stdin terminal attributes */
          tcgetattr(0, &oldtio);
      
          if(sighandler){
          /* Make sure we exit cleanly */
              memset(&sa, 0, sizeof(struct sigaction));
              sa.sa_handler = sighandler;
              sigaction(SIGINT, &sa, NULL);
              sigaction(SIGQUIT, &sa, NULL);
              sigaction(SIGTERM, &sa, NULL);
          }
      
          /* This is needed to be able to tcsetattr() after a hangup (Ctrl-C)
           * see tcsetattr() on POSIX
           */
          memset(&sa, 0, sizeof(struct sigaction));
          sa.sa_handler = SIG_IGN;
          sigaction(SIGTTOU, &sa, NULL);
      
          /* Set non-canonical no-echo for stdin */
          tcgetattr(0, &curtio);
          curtio.c_lflag &= ~(ICANON | ECHO);
          tcsetattr(0, TCSANOW, &curtio);
      }
      
      void term_restore(){
          /* restore terminal attributes */
          tcsetattr(0, TCSANOW, &oldtio);
      }
      
      static char get[4]= {0};
      bool kbhit(){
          struct pollfd pfds[1];
          int ret;
          memset(get, 0, sizeof(char) * 4);
      
          /* See if there is data available */
          pfds[0].fd = 0;
          pfds[0].events = POLLIN;
          ret = poll(pfds, 1, 0);
      
          /* Consume data */
          if (ret > 0) {
              read(0, get, 3);
              return strlen(get) > 0;
          }
          return false;
      }
      
      bool keydown(const char* key){
          return !strcmp(get, key);
      }
      
      it works perfect for keyboard inputs for active application, but application purpose is to get all OS key inputs and interpret it for further use
      
      main.c
      
      #include "linux-kbhit.h"
      #include "stdio.h"
      #include <signal.h>
      
      static sig_atomic_t end = 0;
      static void sighandler(int signo)
      {
          end = 1;
          printf("good beye!\n");
      }
      
      int main()
      {
          term_setup(sighandler);
      
          while (!end) {
              if (kbhit()) {
                  if (keydown(ESC))
                      printf("This is \"ESC\" button!\n");
                  if (keydown(UP))
                      printf("This is \"UP\" button!\n");
                  if (keydown(DOWN))
                      printf("This is \"DOWN\" button!\n");
                  if (keydown(LEFT))
                      printf("This is \"LEFT\" button!\n");
                  if (keydown(RIGHT))
                      printf("This is \"RIGHT\" button!\n");
                  if (keydown(A))
                      printf("This is \"A\" button!\n");
              }
          }
      
          term_restore();
      
          return 0;
      }
      

      Is there a way to get something similar to /dev/input (which is helpful) in Qt Library?
      https://thehackerdiary.wordpress.com/2017/04/21/exploring-devinput-1/

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 10 Aug 2023, 13:00 last edited by
      #2

      @JacobNovitsky said in always get key inputs from application (if app even not active):

      Is there a way to get something similar to /dev/input (which is helpful) in Qt Library

      No but nobody stops you from using this in your Qt app

      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
      0
      • J JacobNovitsky
        10 Aug 2023, 12:44

        Having some app, made with x11 libs, tested for ubuntu. It takes simple keyboard inputs and outputs corresponding value. It works fine if application window (ubuntu terminal) is currently open

        linux-kbhit.h
        
        #define     ESC     "\033"
        #define     UP      "\033[A"
        #define     DOWN    "\033[B"
        #define     LEFT    "\033[D"
        #define     RIGHT   "\033[C"
        #define     A   "a"
        
        void term_setup(void (*sighandler)(int));
        void term_restore();
        bool kbhit();
        bool keydown(const char* key);
        
        linux-kbhit.c
        
        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
        
        #include <unistd.h>
        #include <poll.h>
        #include <signal.h>
        #include <termios.h>
        #include <sys/ioctl.h>
        
        static struct termios oldtio;
        static struct termios curtio;
        
        void term_setup(void (*sighandler)(int)){
            struct sigaction sa;
        
            /* Save stdin terminal attributes */
            tcgetattr(0, &oldtio);
        
            if(sighandler){
            /* Make sure we exit cleanly */
                memset(&sa, 0, sizeof(struct sigaction));
                sa.sa_handler = sighandler;
                sigaction(SIGINT, &sa, NULL);
                sigaction(SIGQUIT, &sa, NULL);
                sigaction(SIGTERM, &sa, NULL);
            }
        
            /* This is needed to be able to tcsetattr() after a hangup (Ctrl-C)
             * see tcsetattr() on POSIX
             */
            memset(&sa, 0, sizeof(struct sigaction));
            sa.sa_handler = SIG_IGN;
            sigaction(SIGTTOU, &sa, NULL);
        
            /* Set non-canonical no-echo for stdin */
            tcgetattr(0, &curtio);
            curtio.c_lflag &= ~(ICANON | ECHO);
            tcsetattr(0, TCSANOW, &curtio);
        }
        
        void term_restore(){
            /* restore terminal attributes */
            tcsetattr(0, TCSANOW, &oldtio);
        }
        
        static char get[4]= {0};
        bool kbhit(){
            struct pollfd pfds[1];
            int ret;
            memset(get, 0, sizeof(char) * 4);
        
            /* See if there is data available */
            pfds[0].fd = 0;
            pfds[0].events = POLLIN;
            ret = poll(pfds, 1, 0);
        
            /* Consume data */
            if (ret > 0) {
                read(0, get, 3);
                return strlen(get) > 0;
            }
            return false;
        }
        
        bool keydown(const char* key){
            return !strcmp(get, key);
        }
        
        it works perfect for keyboard inputs for active application, but application purpose is to get all OS key inputs and interpret it for further use
        
        main.c
        
        #include "linux-kbhit.h"
        #include "stdio.h"
        #include <signal.h>
        
        static sig_atomic_t end = 0;
        static void sighandler(int signo)
        {
            end = 1;
            printf("good beye!\n");
        }
        
        int main()
        {
            term_setup(sighandler);
        
            while (!end) {
                if (kbhit()) {
                    if (keydown(ESC))
                        printf("This is \"ESC\" button!\n");
                    if (keydown(UP))
                        printf("This is \"UP\" button!\n");
                    if (keydown(DOWN))
                        printf("This is \"DOWN\" button!\n");
                    if (keydown(LEFT))
                        printf("This is \"LEFT\" button!\n");
                    if (keydown(RIGHT))
                        printf("This is \"RIGHT\" button!\n");
                    if (keydown(A))
                        printf("This is \"A\" button!\n");
                }
            }
        
            term_restore();
        
            return 0;
        }
        

        Is there a way to get something similar to /dev/input (which is helpful) in Qt Library?
        https://thehackerdiary.wordpress.com/2017/04/21/exploring-devinput-1/

        J Offline
        J Offline
        JacobNovitsky
        wrote on 10 Aug 2023, 13:31 last edited by
        #3

        @JacobNovitsky said in always get key inputs from application (if app even not active):

        Having some app, made with x11 libs, tested for ubuntu. It takes simple keyboard inputs and outputs corresponding value. It works fine if application window (ubuntu terminal) is currently open

        linux-kbhit.h
        
        #define     ESC     "\033"
        #define     UP      "\033[A"
        #define     DOWN    "\033[B"
        #define     LEFT    "\033[D"
        #define     RIGHT   "\033[C"
        #define     A   "a"
        
        void term_setup(void (*sighandler)(int));
        void term_restore();
        bool kbhit();
        bool keydown(const char* key);
        
        linux-kbhit.c
        
        #include <stdio.h>
        #include <stdlib.h>
        #include <string.h>
        
        #include <unistd.h>
        #include <poll.h>
        #include <signal.h>
        #include <termios.h>
        #include <sys/ioctl.h>
        
        static struct termios oldtio;
        static struct termios curtio;
        
        void term_setup(void (*sighandler)(int)){
            struct sigaction sa;
        
            /* Save stdin terminal attributes */
            tcgetattr(0, &oldtio);
        
            if(sighandler){
            /* Make sure we exit cleanly */
                memset(&sa, 0, sizeof(struct sigaction));
                sa.sa_handler = sighandler;
                sigaction(SIGINT, &sa, NULL);
                sigaction(SIGQUIT, &sa, NULL);
                sigaction(SIGTERM, &sa, NULL);
            }
        
            /* This is needed to be able to tcsetattr() after a hangup (Ctrl-C)
             * see tcsetattr() on POSIX
             */
            memset(&sa, 0, sizeof(struct sigaction));
            sa.sa_handler = SIG_IGN;
            sigaction(SIGTTOU, &sa, NULL);
        
            /* Set non-canonical no-echo for stdin */
            tcgetattr(0, &curtio);
            curtio.c_lflag &= ~(ICANON | ECHO);
            tcsetattr(0, TCSANOW, &curtio);
        }
        
        void term_restore(){
            /* restore terminal attributes */
            tcsetattr(0, TCSANOW, &oldtio);
        }
        
        static char get[4]= {0};
        bool kbhit(){
            struct pollfd pfds[1];
            int ret;
            memset(get, 0, sizeof(char) * 4);
        
            /* See if there is data available */
            pfds[0].fd = 0;
            pfds[0].events = POLLIN;
            ret = poll(pfds, 1, 0);
        
            /* Consume data */
            if (ret > 0) {
                read(0, get, 3);
                return strlen(get) > 0;
            }
            return false;
        }
        
        bool keydown(const char* key){
            return !strcmp(get, key);
        }
        
        it works perfect for keyboard inputs for active application, but application purpose is to get all OS key inputs and interpret it for further use
        
        main.c
        
        #include "linux-kbhit.h"
        #include "stdio.h"
        #include <signal.h>
        
        static sig_atomic_t end = 0;
        static void sighandler(int signo)
        {
            end = 1;
            printf("good beye!\n");
        }
        
        int main()
        {
            term_setup(sighandler);
        
            while (!end) {
                if (kbhit()) {
                    if (keydown(ESC))
                        printf("This is \"ESC\" button!\n");
                    if (keydown(UP))
                        printf("This is \"UP\" button!\n");
                    if (keydown(DOWN))
                        printf("This is \"DOWN\" button!\n");
                    if (keydown(LEFT))
                        printf("This is \"LEFT\" button!\n");
                    if (keydown(RIGHT))
                        printf("This is \"RIGHT\" button!\n");
                    if (keydown(A))
                        printf("This is \"A\" button!\n");
                }
            }
        
            term_restore();
        
            return 0;
        }
        

        Is there a way to get something similar to /dev/input (which is helpful) in Qt Library?
        https://thehackerdiary.wordpress.com/2017/04/21/exploring-devinput-1/

        can you please clarify?
        If I use /dev/input I could not use Qt app? or what?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SimonSchroeder
          wrote on 11 Aug 2023, 07:31 last edited by
          #4

          Qt does not provide anything comparable. But, you can write a Qt app which on Linux uses /dev/input. This, however, makes it non-portable. With corresponding #ifdefs you could write the same for other OSes and have the same functionality on all platforms if this is desired.

          J 1 Reply Last reply 28 Aug 2023, 02:49
          2
          • S SimonSchroeder
            11 Aug 2023, 07:31

            Qt does not provide anything comparable. But, you can write a Qt app which on Linux uses /dev/input. This, however, makes it non-portable. With corresponding #ifdefs you could write the same for other OSes and have the same functionality on all platforms if this is desired.

            J Offline
            J Offline
            JacobNovitsky
            wrote on 28 Aug 2023, 02:49 last edited by JacobNovitsky
            #5

            @SimonSchroeder this Qt app seem to be working as I requested, I post later after tests
            https://github.com/Skycoder42/QHotkey

            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