https://unix.stackexchange.com/a/293941
read -n 1 -s -r -p "Press any key to continue"
print("Press any key to continue")
_ = getch()
https://unix.stackexchange.com/a/293941
read -n 1 -s -r -p "Press any key to continue"
print("Press any key to continue")
_ = getch()
// getch() equivalent in Swift: read a single character from stdin without a newline | |
// https://stackoverflow.com/a/59795707 | |
extension FileHandle { | |
func enableRawMode() -> termios { | |
var raw = termios() | |
tcgetattr(self.fileDescriptor, &raw) | |
let original = raw | |
raw.c_lflag &= ~UInt(ECHO | ICANON) | |
tcsetattr(self.fileDescriptor, TCSADRAIN, &raw) | |
return original | |
} | |
func restoreRawMode(originalTerm: termios) { | |
var term = originalTerm | |
tcsetattr(self.fileDescriptor, TCSADRAIN, &term) | |
} | |
} | |
func getch() -> UInt8 { | |
let handle = FileHandle.standardInput | |
let term = handle.enableRawMode() | |
defer { handle.restoreRawMode(originalTerm: term) } | |
var byte: UInt8 = 0 | |
read(handle.fileDescriptor, &byte, 1) | |
return byte | |
} |