0

I am trying to build Tetris Game similar to javidx9 tutorial in C++, but using Swift Command Line Tool in Xcode. I do not plan to use any GUI and display game layout as String of characters. To play the game I need to read Arrow keys inputs while app is running in console. After long reading and googling for solutions I was only been able to find solutions that work only with GUI using keyDown function and NSEvent library.

I need help with getting keyboard inputs (arrow keys, Shift,Command key) in Command Line?

  • Please excuse my lack of experience, I am still in the learning process. I never had this problem using any other language (Python, Java, C++), I do not know why it is hard to find relevant resource for doing this in Swift and XCode*

Thank you!

Kamran Maximoff
  • 969
  • 3
  • 4

1 Answers1

1

I think you could use C API in swift to do that.

C code is from https://stackoverflow.com/a/1798833/1261661

This is just a swift version.

import Foundation

var old = termios()
var new = termios()
tcgetattr(STDIN_FILENO, &old)

new = old
new.c_lflag &= ~(UInt(ICANON))
new.c_lflag &= ~(UInt(ECHO))

tcsetattr(STDIN_FILENO, TCSANOW, &new)

while true {
    let c = getchar()
    if c == 113 {
        // quit on 'q'
        break
    } else {
        print(c)
    }
}

tcsetattr(STDIN_FILENO, TCSANOW, &old)

NSEvent.addGlobalMonitorForEvents can get all the key events too but it require user's permission.

import Cocoa
import Foundation

@discardableResult
func acquirePrivileges() -> Bool {
    
    let accessEnabled = AXIsProcessTrustedWithOptions([kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String: true] as CFDictionary)
    
    if accessEnabled != true {
        print("You need to enable the keylogger in the System Prefrences")
    }
    
    return accessEnabled
}

class AppDelegate: NSObject, NSApplicationDelegate {
    private var monitor: Any?
    func applicationDidFinishLaunching(_ notification: Notification) {
        acquirePrivileges()
        monitor = NSEvent.addGlobalMonitorForEvents(matching: .keyDown) { event in
            print(event)
        }
    }
}

print(Bundle.main)

// Turn off echo of TTY
var old = termios()
var new = termios()
tcgetattr(STDIN_FILENO, &old)

new = old
new.c_lflag &= ~(UInt(ICANON))
new.c_lflag &= ~(UInt(ECHO))

tcsetattr(STDIN_FILENO, TCSANOW, &new)

let appDelegate = AppDelegate()
NSApplication.shared.delegate = appDelegate
NSApp.activate(ignoringOtherApps: true)
NSApp.run()

// restore tty
tcsetattr(STDIN_FILENO, TCSANOW, &old)

You can also take a look at this library, https://github.com/SkrewEverything/Swift-Keylogger I think it is another interesting way to do the job.

  • Thank you! I assume there is no native to swift I/0 access in console? – Kamran Maximoff Nov 11 '21 at 11:01
  • 1
    There is no easy way to do that. NSEvent.addGlobalMonitorForEvents could do the work, also HID API is another way, for your reference, https://github.com/SkrewEverything/Swift-Keylogger. – linkbreaker Nov 11 '21 at 13:41
  • 1
    I don't think `NSEvent.addGlobalMonitorForEvents` is the right way to go. It's essentially a keylogger, which is totally unnecessary because the terminal emulator your program is running in *already accepts input*. I know for one that personally, I would absolutely deny this permission to an app that doesn't have a good reason to use it. It's shifty as heck. – Alexander Nov 11 '21 at 13:57