I am using C in Fedora Linux to build a voice streaming application. I have audio running between two clients, but the next stage is to implement the user interface.
I am aiming to use different keyboard keys in a "push to talk" style, ie holding the "Q" key allows the user to talk to one user, "W" another and so on.
My question is, how would I go about implementing this? The transmit thread is just a while loop that reads 180 bytes from the sound card and sends it as a UDP packet. THe mist obvious issue is echoing of the key pressed, filling the screen with q's and w's, and how I can detect key down/key up in C. I am looking at ncurses but it is a big topic!
Any ideas or further reading would be greatly appreciated.
J
The first part of your question, as to how to detect keypress without using ncurses is answered excellently, using termios, by #jim mcnamara
And ncurses doesn't seem to be as scary as it sounds :-). Here is an ncurses implementation which exactly ( almost ) satisfies your requirement. But according to this post, you need to add a notimeout() call so that getch()(ncurses one) doesn't wait for next keypress.
Related
I was wondering if it was possible to change the key repetition behaviour(on Linux) in such a way that instead of it starting to spam one key as input it goes through all the pressed keys instead (perhaps system("something")?) So instead of when pressing two keys the keys going "wssssssssss", it should go like "wswswswswswsws" instead.
Yes, if you implement an Input Event daemon that grabs (for exclusive access) that keyboard input event device, and produces suitable autorepeat events for keys being pressed long enough, emitting press, autorepeat, and release events back to the system via the uinput device.
I am not aware of anything that already does that, but I estimate it would be less than 500 lines of code in C to implement such a daemon. Such a daemon would need superuser privileges ("run as root"), though.
For command-line game or utility that needs to detect multiple keypresses, using ncurses is way simpler. For graphical applications, the widget toolkit (GTK+, Qt, SDL2, Tcl/Tk, FLTK, and so on) do support multiple keypress detection natively.
If you wanted to write C code without ncurses, you can use termios to control the terminal interface, turn off echoing and line buffering, and achieve the same thing as ncurses – but ncurses also provides nice terminal output functions, and works on basically all terminals and emulators, too.
I have barcode scanner, attached to Linux computer via USB. The scanner emulates a keyboard device.
I have to write a program that to read the scanned barcodes and process them. The program runs on background as a service and should read the barcode scanner regardless of the current X focus.
How this can be made in Linux?
Some lower level solution/explanation is preferred.
It sounds like you want to capture the data from a specified device,
In which case the method described in this post should help:
(EDIT: original link dead, Archive link provided)
https://web.archive.org/web/20190101053530/http://www.thelinuxdaily.com/2010/05/grab-raw-keyboard-input-from-event-device-node-devinputevent/
That will listen out for keyboard events stemming from only the specified source.
A word of caution though, as far as I know, that won't stop it from propagating to whatever your current window focus is.
To start with solution, I guess a daemon would perfect choice.
You can write a daemon code, which will open device node (for scanner) and read the data buffer.
Now you have received data in user space, you are free to handle it as per your requirement.
I have to write a linux kernel module, which change character printed on the screen after pressing a key (let's say - I want 'a' to be printed when I press 'b' on keyboard). What is the best way to do it? I'he thought that good idea is to create module for keyboard. I did some research, i saw few keyloggers (as kernel modules) but all of them where able only to listen what key was pressed, and any change of scancode was imposible (which is pretty obvious in keyloggers). I read 3rd chapter from Linux Device Drivers, and i started to read 6th, but they are talking only about virtual devices, when i want to connect my module with the real device. I saw also this https://stackoverflow.com/questions/33836541/linux-kernel-how-to-capture-a-key-press-and-replace-it-with-another-key, but it wasn't working. My teacher said there is much eaysier way than using interrupts, but I have no idea how to do it (neither using interrupts and any other way).
What should I do? just read next chapters of LDD? Or any other book? Or maybe just lie down and cry?
Here's a very simple example of keyboard driver kernel module: https://github.com/raleighlittles/Olympus-MAJ-1428-Keyboard-Linux-driver/blob/main/hid-olympus-maj1428.c
I had a keyboard that generated weird scancodes for certain non-character keys (read the file, it explains more). Instead of using those scancodes, I wrote code to remap them to the extended function keys (F13, F14, etc.). You could use the code and instead simply switch the scan codes that you want by changing the key_mapping variable.
I'm writing a little pong clone with ncurses and C.
I need to find a way to set the keyboard repeat delay to make it easier for a player to move the paddle, otherwise they are stuck hitting the down key and waiting about 50 milliseconds until the key begins to repeat.
I've checked the ncurses man pages and documentation, and I can't seem to find any reference to changing keyboard repeat delays, also the program kbdrate is not suitable for my use, because it would be a bit strange for a game to ask to be run as root.
Thanks,
Emmanuel
How about capturing the keydown and the repeatedly checking to see if the key is up yet?
In the C language, using keyboard interrupt, how can I display an alternate key from what the user typed? E.g., when I press 'Q' on the keyboard, then the screen should display 'A'.
Handling keyboard interrupt is not a good idea on any platform.
What about usb keyboards, there is no interrupt you can catch ?
For Windows OS's you probably want to write filter driver, you can find this replay partly relevant.
Sample of keyboard filter driver can be found here.
For Linux you probably need to patch HID layer driver. Mac is out my scope completely :)
We need a little more information about Your environment.
As far as I understand Your question, You want to replace the
keyboard interrupt handler with Your own in this way:
Save the entry address of the original keyboard handler.
Install Your own that calls the original and manipulates the keycode value.
However, on most modern operating systems it is much easier to define and install a new keyboard layout.