Search code examples
keyboardstdinnim-lang

How do I read all keys pressed in nim since the last call to a function, without needing to press enter


I need to read keys from stdin without blocking, and without only recieving them when a newline is pressed. The way i would like it be formatted is a sequence, where each item is a keypress or ascii char, of every key pressed since the last time I called this procedure.

I tried the following code:

while true:
    let ch = readChar(stdin)
    echo ch

but the keys are only recieved after I press a newline, and then they come in 1 by 1.


Solution

  • Have you tried to https://github.com/johnnovak/illwill ?

    Something like :

    import os, illwill
    
    illwillInit()
    
    while true:
      var key = getKey()
      case key
      of Key.None: discard
      of Key.Escape, Key.Q:
        break
      else:
        echo "Key pressed: ", $key
      
      sleep(20)