PDA

View Full Version : Setting up hotkeys in console apps



Geek on Skates
12-08-2018, 11:04 PM
Hey everyone, new guy here;

I've been hooked on Linux lately, and I have a project I'd like to build, but it would require creating hotkeys/keyboard shortcuts. Looking at programs like nano or calcurse, it's obvious this is possible, but I haven't been able to find any kind of tutorial or other documentation explaining how. The little bits I've found are more for desktop environments like KDE, and even those are few and far between. So any info on the subject would be awesome! Thanks in advance. :)

PS: On a different note, is there some big website I'm missing? Like when developing Windows apps I can always to to MSDN for more info; does anything similar exist for Linux?

GReaper
12-09-2018, 12:37 AM
What do you want to do? Give us some examples.

Geek on Skates
12-09-2018, 04:15 AM
Sure. If you type "nano /path/to/some/file.ext" you can use the arrow keys to move the cursor around, and press CTRL + X to exit. Both of these are examples of what I'm trying to do. I know how to move the cursor using ASCII escape codes, but how would I set up the arrow keys as a hotkey? Another good example is the menu in wordgrinder. All these things run in the terminal, but give users keyboard shortcuts (like CTRL + X, or Escape, or whatever) to do stuff. That is what I would like my program to do. In Win32 there is a function called RegisterHotKey for this sort of thing, but I'm not sure what that would look like on Linux.

Geek on Skates
12-09-2018, 03:18 PM
Just found a working example here:

https://pacificsimplicity.ca/blog/epoll-devinput-keyboard-press-example

It's not exactly what I was trying to do, but I could use it to implement keyboard shortcuts; hopefully you get what I mean by hotkeys or shortcuts by now. Even simple things like input requests ending in "[y/n[:" - you don't have to press Enter, the text isn't displayed anywhere, it just detects that you pressed Y and does its thing. I found another program that uses shortcuts, "wordgrinder" - you hit Escape to go to a menu bar, then press letters to navigate the submenus. Anyway, just thought I'd put it here in case anyone else has teh same question - or in case there is a better way. :)

Codeplug
12-10-2018, 07:16 AM
Better reading here: https://cboard.cprogramming.com/c-programming/157438-capturing-keyboard-input-one-character-time.html#post1165318

gg