Keyboard Shift and Control Keys during get_single_char/1 in linux terminal

I have a terminal based menu system using get_single_char/1 with with_tty_raw/1 where the user navigates with arrows code 65-68… But I’d like to add to their navigation by letting them use ctrl-arrows and shift arrows… The codes dont change when the ctrl and shift are used. Although it might be my terminal client (putty.exe) is intercepting.

Is there a method for knowing if the shift/ctrl key is being held down while user presses these keys?

Also weirdly PageUp and PageDown seem to return the same code (126). Any advice?

Ah I see I was too quickly to convert the codes into a char atom… basically I was able to get things working with

get_single_key_code(Code):- get_single_char(C),into_key_codes([C],Code).

into_key_codes([27],Codes):- get_single_char(C1),into_key_codes([27,C1],Codes).
into_key_codes([27,27],Codes):- get_single_char(C1),into_key_codes([27,27,C1],Codes).
into_key_codes([27,91],Codes):- get_single_char(C1),into_key_codes([27,91,C1],Codes).
into_key_codes([27,79],Codes):- get_single_char(C1),into_key_codes([27,79,C1],Codes).
into_key_codes([27,N1,54],Codes):- get_single_char(C1),into_key_codes([27,N1,54,C1],Codes).
into_key_codes([27,N1,53],Codes):- get_single_char(C1),into_key_codes([27,N1,53,C1],Codes).
% crl left arrow
do_menu_codes([27,79,68]):- !, previous_test, print_test.
% ctrl right arrow
do_menu_codes([27,79,67]):- !, next_test, print_test.
% alt left arrow
do_menu_codes([27,27,79,68]):- !, previous_test, print_test.
% alt right arrow
do_menu_codes([27,27,79,67]):- !, next_test.
% left arrow
do_menu_codes([27,91,68]):- !, previous_test.
% right arrow
do_menu_codes([27,91,67]):- !, next_test, print_test.
% page up
do_menu_codes([27,91,53,126]):- restart_suite
% page down
do_menu_codes([27,91,54,126]):- next_suite.

You may not be reading the pending codes,

see the answer here:

1 Like

You don’t need these together. get_single_char/1 reads a single character in raw mode and is intended for single character replies, such as the toplevel and debugger. with_tty_raw/1 keeps the terminal in raw mode and thus you can use get_code/1, read_pending_codes/3, etc.