LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-23-2018, 10:01 PM   #1
xucaen
Member
 
Registered: Feb 2004
Location: Boston
Distribution: Kubuntu 22.04
Posts: 101

Rep: Reputation: 16
Question How to detect ALT+X keypress with NCURSES?


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?
 
Old 02-24-2018, 02:32 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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
 
2 members found this post helpful.
Old 02-24-2018, 01:44 PM   #3
xucaen
Member
 
Registered: Feb 2004
Location: Boston
Distribution: Kubuntu 22.04
Posts: 101

Original Poster
Rep: Reputation: 16
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:

(pseudocode)

Code:
key = wgetch();
if(key == 27)
{
  key = wgetch();
  if(key == X)
   {
      do my alt+x stuff here;
   }
}
... I am able to capture the ALT+X combination.

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?
 
Old 02-24-2018, 02:10 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Should I repeat my previous post?
 
1 members found this post helpful.
Old 02-24-2018, 02:18 PM   #5
xucaen
Member
 
Registered: Feb 2004
Location: Boston
Distribution: Kubuntu 22.04
Posts: 101

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by NevemTeve View Post
Should I repeat my previous post?

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. ;-)
 
Old 02-24-2018, 04:33 PM   #6
xucaen
Member
 
Registered: Feb 2004
Location: Boston
Distribution: Kubuntu 22.04
Posts: 101

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by NevemTeve View Post
Should I repeat my previous post?

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.
 
Old 02-25-2018, 04:13 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
There is another test-program, demo_keyok, it prints the return value of wgetch. For ALT+x and ALT+SHIFT+X it prints these lines:
Code:
Keycode 27, name ^[
Keycode 120, name x
Keycode 27, name ^[
Keycode 88, name X
Now I add this into the program (I pick value 1024 for Alt):
Code:
#define Alt_X (1024+'X') /* 1112 == 0x458 */
#define Alt_x (1024+'x') /* 1144 == 0x478 */

define_key ("\033X", Alt_X);
define_key ("\033x", Alt_x);
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>

Last edited by NevemTeve; 02-25-2018 at 04:32 AM.
 
Old 02-25-2018, 09:17 AM   #8
xucaen
Member
 
Registered: Feb 2004
Location: Boston
Distribution: Kubuntu 22.04
Posts: 101

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by NevemTeve View Post
There is another test-program, demo_keyok, it prints the return value of wgetch. For ALT+x and ALT+SHIFT+X it prints these lines:
Code:
Keycode 27, name ^[
Keycode 120, name x
Keycode 27, name ^[
Keycode 88, name X
Now I add this into the program (I pick value 1024 for Alt):
Code:
#define Alt_X (1024+'X') /* 1112 == 0x458 */
#define Alt_x (1024+'x') /* 1144 == 0x478 */

define_key ("\033X", Alt_X);
define_key ("\033x", Alt_x);
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.
 
1 members found this post helpful.
Old 02-25-2018, 10:00 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> 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.
 
  


Reply

Tags
ncurses



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cursing ncurses ... reverted to Slackware 14.0 version of ncurses 5.9 aaazen Slackware 3 06-18-2014 11:14 AM
Disable Shutdown Request on Control-Alt-Delete KeyPress marsigme Linux - Newbie 6 11-02-2012 03:49 PM
detect keypress without needing return knobby67 Programming 1 10-21-2010 07:36 AM
how to print text in color by ncurses without opening a ncurses window Greatrebel Programming 0 12-20-2006 09:15 AM
how do i read <alt>+<...> with ncurses? Rodrigo Renie Programming 1 06-21-2004 02:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration