always get key inputs from application (if app even not active)
-
wrote on 10 Aug 2023, 12:44 last edited by
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/ -
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/@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
-
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/wrote on 10 Aug 2023, 13:31 last edited by@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? -
wrote on 11 Aug 2023, 07:31 last edited by
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
#ifdef
s you could write the same for other OSes and have the same functionality on all platforms if this is desired. -
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
#ifdef
s you could write the same for other OSes and have the same functionality on all platforms if this is desired.wrote on 28 Aug 2023, 02:49 last edited by JacobNovitsky@SimonSchroeder this Qt app seem to be working as I requested, I post later after tests
https://github.com/Skycoder42/QHotkey