Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling joint key presses with Haskell

Writing a program in Haskell, I am struggling to handle key presses of the form ctrl-s and ctrl-l

I am using the following code:

main :: IO ()
main = do
  hSetBuffering stdin NoBuffering
  x <- getChar
  putStrLn ("You pressed: " ++ [x])

How can I make it recognise the Ctrl button being pressed?

like image 781
Stuart Paton Avatar asked Jan 01 '26 08:01

Stuart Paton


1 Answers

getChar gives you access to characters, not keypresses. Which character you get depends on your user's operating system, keyboard layout, and choice of input method. There is no 'standard' Character which will be generated by the keypresses Ctrl-S or Ctrl-L (although, certainly, some systems will give the standard ASCII codes for those control characters, others will not).

If you want proper keypress handling you need a real input library - like, for example, SDL or WxWidgets or GTK; each of which is much more than just an input library but they do have keypress abstractions.

like image 130
drquicksilver Avatar answered Jan 04 '26 07:01

drquicksilver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!