PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, December 19, 2022

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

 December 19, 2022     haskell, syntax     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing