So, I need a global way to grabbing keycodes, when they press and release.
By the history of my searching I found some other, local ways to grab keys, like <Termios.h> which grabbing keys BY pressing keys in terminal, X11 which grabbing keys BY pressing them in window
And I have some Ambiguous problems to which I would like to hear an answer. Question sheet at the end, notes here.
I often will be refer to the code from some apps, so, full-code:
(1)showkey_test.c:
#include<stdio.h>
#include<termios.h>
#include<fcntl.h>
#include<unistd.h>
int fd;
struct termios old;
int binary_stdout(int code)
{
if(code > 0xff)
{
printf("|Not good");
}
else if(code <0xff)
{
int i =0;
printf("|");
while(i < 8)
{
printf((code & 0x80) ? "1":"0");
code = code<<1;
i++;
}
}
}
int main()
{
int n, i;
struct termios new;
unsigned char buf[16];
fd = 0;
if(tcgetattr(fd, &old)==-1)
{
printf("Old tcgetattr");
}
if(tcgetattr(fd, &new)==-1)
{
printf("New tcgetattr");
}
new.c_lflag &= ~ (ICANON | ISIG);
new.c_lflag |= (ECHO | ECHOCTL);
new.c_iflag =0;
new.c_cc[VMIN] = 1;
new.c_cc[VTIME]=0;
if(tcsetattr(fd, TCSAFLUSH, &new) == -1)
{
printf("Tcgetattr");
}
while(1)
{
n = read(fd, buf, 1);
if(n == 1)
{
binary_stdout(buf[0]); //AKA buf[0] := **char?
printf("\t%3d 0%03o 0x%02x\n", buf[0], buf[0], buf[0]);
#ifdef ANYL_STR
printf("%d[0]\n", buf[0]);
#endif
if( n!=1 || buf[0] == 04)
{
break;
}
}
}
}
Try to grab key global from keyboard by the DRIVER. Doesn't work also with correct event and int. It code from box
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <linux/input.h>
#include <time.h>
#include <stdint.h>
int main(int argc, char* argv[]){
sleep(1);
int rcode = 0;
char keyboard_name[256] = "Unknown";
int keyboard_fd = open("/dev/input/event1", O_RDONLY | O_NONBLOCK);
if ( keyboard_fd == -1 ) {
printf("Failed to open keyboard.\n");
exit(1);
}
rcode = ioctl(keyboard_fd, EVIOCGNAME(sizeof(keyboard_name)), keyboard_name);
printf("Reading From : %s \n", keyboard_name);
printf("Getting exclusive access: ");
rcode = ioctl(keyboard_fd, EVIOCGRAB, 1);
printf("%s\n", (rcode == 0) ? "SUCCESS" : "FAILURE");
struct input_event keyboard_event;
char mouse_name[256] = "Unknown";
int mouse_fd = open("/dev/input/event2", O_RDONLY | O_NONBLOCK);
if ( mouse_fd == -1 ) {
printf("Failed to open mouse.\n");
exit(1);
}
rcode = ioctl(mouse_fd, EVIOCGNAME(sizeof(mouse_name)), mouse_name);
printf("Reading From : %s \n", mouse_name);
printf("Getting exclusive access: ");
rcode = ioctl(mouse_fd, EVIOCGRAB, 1);
printf("%s\n", (rcode == 0) ? "SUCCESS" : "FAILURE");
struct input_event mouse_event;
int end = time(NULL) + 10;
while ( time(NULL) < end ) {
if ( read(keyboard_fd, &keyboard_event, sizeof(keyboard_event)) != -1 ) {
printf("keyboard event: type %d code %d value %d \n", keyboard_event.type, keyboard_event.code, keyboard_event.value);
}
if ( int sz = read(mouse_fd, &mouse_event, sizeof(mouse_event)) ) {
if(sz != -1) {
printf("mouse event: type %d code %d value %d \n", mouse_event.type, mouse_event.code, mouse_event.value);
}
}
}
printf("Exiting.\n");
rcode = ioctl(keyboard_fd, EVIOCGRAB, 1);
close(keyboard_fd);
rcode = ioctl(mouse_fd, EVIOCGRAB, 1);
close(mouse_fd);
return 0;
}
Soo, my questions: 1.(ABOUT scancodes and char-unsigned/char types) 1.1 Why the code
int binary_stdout(int code)
{
if(code > 0xff)
{
printf("|Not good");
}
else if(code <0xff)
{
int i =0;
printf("|");
while(i < 8)
{
printf((code & 0x80) ? "1":"0");
code = code<<1;
i++;
}
}
}
does print in standart-stream i/o print MORE THAN 8 symbols?
In (int code) transmitted char || unsigned char type. For example out for Alt+f:
^[f|00011011 27 0033 0x1b
|01100110 102 0146 0x66
I understand that's UTF-8 cause linux and cause 0xb1, but... Why the char type containing more than 8 bits? Or When symbol encoding more than 1 byte the char becomes pointer on massive of bytes?.. Cause i don't understand why the cycle repeats itself, when it hardcode only on 8 symbols.
If that is it: can you give a standart in C where it describes? Or maybe, source, for better understanding that encoding phenomens
1.2 Why the scancodes in termios-realisation differing from the driver keyboard-scancodes?
1.3 How Are being formed scancodes for multi-keys, like (^D). In termios it 0x04(if believe showkey_test.c):^D|00000100 4 0004 0x04, but the output of the driver scancodes it's only sequences of pressing bytes like {SCANCODE CTRL} then {SCANCODE D}
1.4 from (1.3 ) Which ways to put scancode 0x04(^D) in file, get ONLY sequences of {SCANCODE CTRL} and then {SCANCODE D}?
2.(ABOUT source for keyloggers/GLobal grabbing keys)
2.1 Where can I get source for see all keyboard-ioctls to grab Keys?
2.2 Can you offer a way to global grabbing keycodes, which pressed and released?It's must be connected with kbd driver
SO, I tried to solve that problem many ways, but I'm totally confused also where I can get a reliable information about it. I hope you will tell me at least where i need to see, and what I need to google. Cause I trying, but it don't giving results