How to intercept keyboard input at the lowest level in linux? - c++

I am interested in writing a program for linux that will read ALL keystrokes, process it and THEN output to the rest of the running processes. Essentially, ALL keyboard input must go into this program and this program alone...Then the program will act as the keyboard for the rest of the computer. I basically want to do something like predictive text on android devices, so my program will act as a filter.
What i'm asking is basically how to direct all keyboard events to my program. While i am not looking for code, i would like to know what part of linux programming/ linux system do i have to learn to be able to complete this task? this, because i am doing this in an attempt to better learn linux.

You shouldn't modify keyboard drivers since this will require you to have a solution for every keyboard manufacturer.(and there are quite a lot of these..)
Instead you should patch a kernel function that is called by all drivers before passing the input further up the stack.
To start with, you could patch input_event which is usually called by all input drivers see documentation here (not only keyboard but also mouse and other devices)
In any case you will have to "decode" the input scan code where you might find this documentation useful.
For more information on kernel patching read here and here.

Related

How to override/disable ncurses input?

I'm writing a code, and I'd like to make it in two variants - with text-based interface (TUI) implemented with ncurses, and with GUI implemented with Qt5. So, passing an argument in the command-line, I can choose which version to run - with GUI or TUI (e.g., just like with YaST in openSUSE). The rest of the code, including inputs from the keyboard, should be independent of which interface (TUI or GUI) is chosen. As I see it, the optimal way to implement such a program is to use distinct classes for input, for user interface, etc. So, in whatever version I run the code, the class handling the input should be the same, and it passes the data to the interface class, which might be TUI or GUI, depending on how the application was launched. The problem is that for ncurses it seems impossible to detach input from the output. Basically, what I want is to still be able to use ncurses output (windows, panels, etc.), but to perform input with some other library. Anyone knows how to solve that? Also, what input C/C++ libraries can I use for reading keyboard event in whatever mode (terminal or GUI)?
You do not have to use ncurses' input-functions (such as wgetch) when using ncurses for output.
A few programs do this (Midnight Commander, vi-like-Emacs) because they use inputs that largely are not resolved to special keys using ncurses, such as
the escape character by itself (vi-like-Emacs), or
the mouse-code in Midnight Commmander (which uses select for monitoring multiple inputs).
Managing different output streams actually can be more difficult, since those use information about the appearance of your program on the computer screen. Inputs usually do not occupy more than a line or so of the screen.
In vi-like-Emacs, the program uses a terminal-driver which knows how to work with a specific type of device (terminal emulator, X windows, Win32 GUI), and the program is compiled and linked with that driver. It would be nice to be able to switch between drivers at runtime, but nuances of mouse-handling are harder to factor out than keyboard input and screen updates.

Reading arrow keys with C++

I am writing an C++ Application and have to read if an arrow key is pressed or not.
I only found some function that are only working on Windows.
You have such problem because you just ask the wrong question. If you application is a command line tool and is accessible from a terminal, than it's just impossible to know which keys are pressed at the moment because the terminal can be far away from the machine where your application runs and which is more important, there is no reason for terminal to send you the arrow key presses because terminal can use them for text navigation.
So you may search how to make the terminal to send you key presses. Not every terminal will support it, but, I think, most of modern terminals in modern OS do.
If you has a gui application that is for running locally and assuming that you control it from the keyboard that is plugged in. Than you should search for the documentation for your gui toolkit. (Qt, wxWidgets, raw xorg, windows API, etc.)
So there are just no native C++ solution for this problem because you question just has no sense in many situations.
So you can use some console library like ncurses or gui toolkit like Qt or search for a native solution in your particular situation, but don't expect this last way will work without any additional code on other machines.
Or just search for other libraries that can allow you to do it.
As you say you only found material for Windows, I assume you are looking for a Linux-Unix way. Old dinosaurs like me remember the time when we only had true consoles (only a keyboard and a 80x25 display). And in these early times existed low-level libraries to interpret keypad transmitted keys and position cursor on screen on almost any terminal, and higher level ones to use the screen as a (text only) GUI.
You should look for curses or ncurses for high level libraries, and terminfo for the low-level capabilities.

Low level keyboard hook doesn't work for a specific device in a specific app

I have coded up a low level keyboard hook using SetWindowsHookEX() on Windows CE 4.2 and it seems to work just fine. I am able to see key events using the keyboard and a barcode scanner in notepad and other applications as desired, but I do not see the barcode scanner events in the one application that I want to collect the keys in. I still see events from the keyboard, however, so I know that the keyboard hook is still working and in the hook chain. I have even tried inserting my hook in the chain every millisecond just to see if it would make a difference, but no dice. If I flip back to notepad, it's back to working the way I want it.
I'm not sure what the other application is doing to gain control of the scanner when that application is active that prevents it from acting like a keyboard any more. Any thoughts on this would be greatly appreciated. I've done a bunch of searches without any success. I looked into trying to use RAWINPUT, but it doesn't seem to be supported in Windows CE 4.2 from what I can tell as I don't see the user32.dll in the SDK.
There are two ways to get barcode data on most WEC devices.
The keyboard wedge (where data comes in as as keyboard events)
An OEM specific barcode reader API
If this other app your looking at uses option #2 then there is no keyboard data to retrieve, so it makes sense you wouldn't see any. That said, you might read this article to see if it offers any tips for your keyboard hook.
Functions exported by user32.dll in big Windows are generally found in coredll.dll in WEC/WEH.
-PaulH

How to fetch data in a background process in Ubuntu

I am finding a Ubuntu OS command, which lets the program to read the data from keyboard even if the program is in background. I tried to search it a lot but got no success. If any Ubuntu/Linux programmer knows the OS command which lets the program to do so, Please share it with me.
I am a beginner of Ubuntu programming.
You can use the Linux input subsystem to read events from mice and keyboards. It will only work if your application has the necessary privileges. Basically, you have to run the application as root for this to work.
If you cannot run as root, you should not be attempting to monitor the keyboard anyway.
You can create an X11 application to monitor keyboard events in the current session. It only works for the current user, and in the current graphical environment, and may not be able to observe privileged dialogs, for example password inputs. For details, look at the application shortcut launcher for your desktop environment; all Linux DEs I've ever heard of have one.
I think the old Linux Journal articles, The Linux USB Input Subsystem and Using the Input Subsystem, are still one of the best introductions to the Linux input subsystem. Most Linux distributions nowadays also support uinput, a similar device that allows injecting input events back to the kernel subsystem, designed to allow user-space input device drivers. Their interfaces are described in /usr/include/linux/input.h and /usr/include/linux/uinput.h. I recommend you start at the above articles, and then look at some input and uinput examples.
If you are comfortable using a program, have a look at Logkeys project
. It directly takes input from /dev/input/event*.

Standard Keyboard Driver Source Code - Where can I find this?

I am looking for the standard source code for a keyboard. Is there any where that I can download this source code so that I can modify it for my own use?
No such thing as "standard source code" for a keyboard. Here's a page on the Linux keyboard driver: http://www.linuxjournal.com/article/1080
Your best chance for a Windows keyboard driver will probably be to get the Windows DDK (Device Driver development Kit). OTOH, based on the questions' tags (especially vb.net) I'm left wondering exactly why you'd want that. The keyboard driver itself mostly just gets activated when the user presses a key, gets the data, and goes back to sleep. If you're interested in something like mapping keys to different characters, that's not in the keyboard driver itself at all (without even looking at any code for the driver itself).