Monday, December 19, 2022

[FIXED] How to compare a char and escape terminal escape sequence

Issue

If I have something like this:

main :: IO ()                    
main = do                        
    hSetBuffering stdin NoBuffering
    c <- getChar                   

How would I go about comparing c to see if I got an escape sequence like the arrow keys or ctrl plus a key.


Solution

You can look up the expected sequences using the terminfo database.

See the module System.Terminfo.Keys.

For example:

Prelude System.Console.Terminfo> t <- setupTermFromEnv 
Prelude System.Console.Terminfo> getCapability t keyLeft
Just "\ESCOD"

As far as the control characters, those will just appear as ASCII controls:

Prelude> getChar -- pressing control L
'\f'
Prelude> '\f' == '\^L'
True

You might like better to use a more complete library for interacting with the terminal like vty.



Answered By - glguy
Answer Checked By - Katrina (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.