1 year ago

#363328

test-img

Smally

Keypress event in Swift command line application

I'm creating a command line application which requires the user to press a single key to input something.

I've heard of the readLine() function, but the user needs to press return in order to enter it and it allows for >1 character inputs. Also, the readLine function stalls the thread it's called on, which I'd like to avoid (so it should be an event/interrupt with a handler closure).

Something like this is what I'm looking for:

var characters = [Character]()
keyPressEvent(handler: { characters.append($0) })
// ... rest of code using 'characters'

How do I implement that in actual Swift? Thanks!

swift

command-line-interface

readline

0 Answers

Your Answer

Accepted video resources