Handle keyboard input on shell - c

I am developing a custom shell like bash (in C).
I am working right now on the events triggered by the arrows and the tab key. However, those special keys should be handled correctly.
What I want is to capture those keys and not let the CLI draw them on screen. What happens is that when the up arrow is pressed, for example, it draws the following symbol: '^[[A'
How can I stop it from happening? And what is the best way to read and map those keys?

It's kind of old, but some shells use terminal capabilities (termcaps) to block output and move the cursor.
You can also use the lib ncurses that is a cool wrapper for termcaps.

Related

How to navigate code on a keyboard with no Home/End keys and no Fn key?

I'm looking at various laptops for one to program on while traveling and I found this one: the Asus Flip Chromebook:
It's already hard to find sub-15" laptops with a Del/PgUp/PgDn/End row which I use religiously while coding but most laptops have at least an "Fn" key to acquire most of this functionality from the arrow keys. But this laptop doesn't even have that.
I understand that this laptop is a Chromebook and isn't targeted at programmers but if someone wanted to use a laptop like this for programming, how can an environment and IDE be setup to account for these missing keys?
For example, to to select the current line I type Shift+Home if I'm at the end, Home+Shift+End if I'm below, etc.
What solutions and different workflows are available for this kind of keyboard?
Official Chromebook Shortcuts
It looks like Google may have you covered here, with their list of Chromebook-specific keyboard shortcuts. They do include shortcuts that work as Home, End, Page Up, Down, Delete.
Editor-specific shortcuts
If you're finding these shortcuts aren't working for your purposes, you can also check to see if the specific cloud IDE has keyboard shortcuts available, such as these for Repl.it. You might want to search up to see if there are keyboard shortcuts available for your preferred editor.
Personal Recommendations if the above options don't help
Some combinations I've noticed work:
Alt+left arrow to replace Home
Alt+right arrow to replace End
You can also add in the shift key here to highlight to the beginning and the end of the line respectively.
Page Up and Down are tricky but if you're using them to go to the top of the doc, you can make use of Ctrl+A which highlights everything. Then you can use the arrow keys to go to the left or right of the total selection:
Ctrl+A then left arrow to go to the top
Ctrl+A then right arrow to go to the bottom
If you're wanting to highlight a selection from the current cursor position to the top or bottom, you could try just holding shift and an up or down arrow key.
Note: With any navigation shortcuts you find, you can try to throw in shift when pressing them to also highlight at the same time, though, there will likely be some trial and error.
To use the Home, Page up, Page down, and End functions hold down the Fn key and press the corresponding arrow key.
Combination Function
Fn key with Left arrow Home
Fn key with Up arrow Page up
Fn key with Down arrow Page down
Fn key with Right arrow End

Clear a character on screen with ncurses

My program currently allows the user to draw the $ character on the screen with ncurses initialized when a key is pressed.
mvaddch(y,x,'$');
I also have a box drawn and I want to say that after the user presses a specific key, I want the $ to be erased and placed in the new position the user puts it without erasing the entire screen. I tried using erase(), but after that it would erase the entire screen and I don't want that. I want it to keep the box that was drawn. So how would I do this?
The usual way to approach this is to create windows on the screen, e.g., with newwin or subwin, and create the box and '$' in different windows while using clear/wclear to clear the appropriate places on the screen.
Keep in mind that getch/wgetch does a refresh on the window with which it is associated, so that may overwrite updates from overlapping windows.

Make a layer in terminal

I'm trying to make a simple autocompletion tool for my program, and i would it look like as this picture : https://github-camo.global.ssl.fastly.net/ac6492f955c9d8027b6f691e1e3df6052fa16599/687474703a2f2f6e6f736d696c65666163652e72752f696d616765732f63636f6465322e706e67
There are termcaps who can help me to make this ? As a little "te" "ti" capabilities ?
Thank you.
Generally, you cannot get the screen contents, because some people view the notion of an escape sequence which can return the screen contents as a security problem.
The xterm ti/te termcap capabilities do not return the information on the screen. Instead, they tell xterm to switch between the normal and alternate screen buffers. But those cover the entire screen -- not a portion of it as your example suggests. Also, these sequences are sent by any conventional application at the beginning and end of "full-screen" mode -- so your application is likely already using the alternate screen.
Instead, your application has to keep track of what it puts on the screen, so that it can repaint after the popup window goes away. That is something that ncurses, for example, is designed to do.

How do we simulate a mouse click with Xlib / C?

I want to find C / Xorg code to 'enter' a left mouse button click. I'd expect a single line of code but the only things I've found written in C are about two dozen lines long and they don't work anyway :( It seems it can be done in Windows, but I'm in Linux.
The reason for the question is that I've written a utility that lets me move my mouse pointer between several screens using the keyboard. The only problem is that if I move to a location where window abc used to be but another window xyz has been loaded on top of that same location, the mouse pointer moves to xyz just fine, but xyz doesn't have focus -- until I left click the mouse. So, I want to build the 'click' into my code.
The code I tried that didn't work was based on XSendEvent().
Yes, I've more or less come to understand. Anyway it seems this is the way:
{
#include <X11/extensions/XTest.h>
XTestFakeButtonEvent(display, 1, True, CurrentTime);
XTestFakeButtonEvent(display, 1, False, CurrentTime);
XFlush(display);
}
... and add " -lXtst " to the LDFLAGS line in the Makefile.
Xlib seems to be so bloody difficult. I've had advice to use other libraries, I wish I knew how to go about changing over.
Thanks R.
Why not just directly raise/focus the window rather than trying to make a fake click event? That should be a lot more reliable and work with all window managers, even non-click-to-focus ones.
xdotool is the easy way of doing this. It's a command line tool. You can use it in simple scripts. For example:
#!/bin/sh
xdotool mousemove x y
xdotool click 1

Xlib: draw a text input box and read text as it is typed

I am trying to implement a text box where a user can type, use arrow keys, backspace, delete, etc. I would like to be able to know what is in this text box without the user needing to submit anything. I suppose I could catch keypress events, find a way to display a cursor, and basically build a min-text-editor by hand--but maybe that would be reinventing the wheel?
What I am after is rather scrabble-like. You have several letters in the top part of a window and a text box in the bottom. Each time you type a letter it disappears from the top pane so that you know when you've used them all up. I want to be able to edit that text with the arrow keys, 'cause rather than the 7 letters scrabble would give me I hope to be doing this with paragraphs.
I have the window displaying, and the source file processed and displayed as a list of allowable letters... I just want to update the list of allowable letters while the user types in their sentence. Can Xlib do this? Is there something else that might be more suitable? Thanks!
Can Xlib do this?Why yes, Xlib can do a lot of things. What you describe seems simple enough by using X's event processing and drawing functions.
Xlib is pretty crufty, though, and IMO you should only use it if you need closeness to the X protocol. (Even then there are newer replacements like XCB. But I digress.)
You might find it easier to work with a modern toolkit, like GTK+ or Qt.
For example, this might be expressed as a GtkEntry with a "key-press-event" handler.

Resources