ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to figure out how to detect the ALT+X keypress using ncurses.
I found this thread https://www.linuxquestions.org/quest...curses-196057/
Which doesn't really answer the question, and elsewhere on the web no one seems to be able to answer the question (at least google isn't showing me viable hits to my queries), so I am starting to think it either can't be done or it is not wise to do so and people find different solutions.
I have some code I wrote a year ago in Windows using PDCurses. In PDCurses they defined an ALT_X constant that I could check after calling wgetch(). But in ncurses, there is no ALT_X defined so I am unsure what to test for. I looked in ncurses.h and see they have defined BUTTON_ALT, but it seems to be tied to the mouse somehow.
So, with ncurses I have no idea how to check for ALT+X, but there must be a way. Does anyone know how to do it?
Key-combination Alt+SomeKey usually generates sequence ESC; SomeKey (might be not true for keys that generate escape-sequences in themself like Fn, Arrows etc)
You can change this behaviour with define_key, see ncurses example-program demo_altkeys.c
I wrote a test program with ncurses, and I see the output of wgetch() when I press ALT+X, it seems the ALT is key 27 (the ESC key as mentioned above) and key 120, which is the 'x' key. Now if I do something like this:
But - I want to map these values to constants. I seem to recall something about doing bitwise operations on numbers to get a unique value, something like 27 & 120 and then I could assign that value to a constant ALT_X. Is there some way to do this so I can get a unique value for every ALT+key combination?
I thought you were only explaining that the ESC key is 27. Did you mean that I can simply obtain the sum of 27+120 to get a unique key value? For some reason I thought there would be more to it than that so I just assumed I didn't understand your comment. ;-)
yes please, can you explain further? I read the code example you linked and did some research. I don't see how I can use define_key() in my situation to capture ALT+x at the keyboard. I am finding a bunch of references to ESC being "27" and "\033" but no explanations how to combine that with "x" to get an ALT+X key combination.
Then when I run it again, ALT+x and ALT+SHIFT+X it prints these lines:
Code:
Keycode 1144, name <null>
Keycode 1112, name <null>
That doesn't help me. Where did you get 1024 from?
Anyways, I ended up doing the if statements that I posted above where I check for 27 and then I do a second get in add the two numbers together and return it. Then I made a constant ALT_X equal to 147. It seems to be working fine and it's a lot simpler and easier to understand.
> Where did you get 1024 from?
Just choose a number above MAX_KEY. Mind you, there is a Meta=0x80 convention in ncurses, but that come from the time of 7-bit ASCII.
Anyways, it's cool that you solved it on your own. Well done.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.