0

I'm trying to write a Swift program which accepts input from the keyboard --- specifically, I want the computer to act as soon as a single key is pressed. So the user doesn't have to press Enter after the keypress.

How can I do this?

Swift Developer
  • 247
  • 2
  • 18
  • Possible duplicate of [Check if the u key is pressed Swift Cocoa](http://stackoverflow.com/questions/36911418/check-if-the-u-key-is-pressed-swift-cocoa) – Thomas Dickey May 21 '16 at 11:59
  • Okay so I put the following code in my an empty main.swift file. Then I run the program but nothing happens. import AppKit func keyDown(theEvent: NSEvent) { if let keyString = theEvent.charactersIgnoringModifiers where keyString == "u" || keyString == "U" { Swift.print("BUT...BUT…") } } – James Schneider May 21 '16 at 13:03
  • Are you typing into a text input field or just using key commands? – owlswipe May 21 '16 at 15:12
  • @John I don't think I understand your question. As an experiment I added the following code --- var goof : NSEvent keyDown(goof) This gives the error that I'm using the variable goof before it is initialized but I don't know how to initialize an NSEvent – James Schneider May 21 '16 at 16:19
  • Ok. It's probably my fault that you don't recognize that, since I'm an iOS developer and didn't realize this was a Mac question until too late... – owlswipe May 21 '16 at 19:45
  • This might help you (sorry that it's in objc): http://stackoverflow.com/questions/6164471/listen-to-a-value-change-of-my-text-field – owlswipe May 21 '16 at 19:45

0 Answers0