11

I have been working on a Cocoa OSX project in swift that requires the use of keyboard input to preform an action. On keydown i want to move an object across the window, but stop the object as soon as the key is let up. I have looked in the documentation for AppKit and found the KeyDown function but I cannot seem to figure out how to use it. I want to create a function to call in my game update timer that will preform this. Thanks

import Cocoa
import Appkit

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

@IBOutlet weak var window: NSWindow!

func applicationDidFinishLaunching(aNotification: NSNotification) {
    // Insert code here to initialize your application

     func keyDown(theEvent: NSEvent) {
        if (theEvent.keyCode == 1){
            println("test")
        }

    }

}

func applicationWillTerminate(aNotification: NSNotification) {
    // Insert code here to tear down your application



}

}

  • read Apple guides especially this one https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/EventOverview/HandlingKeyEvents/HandlingKeyEvents.html "Handling Key Events". You will also find in it sample codes. – Jean-Baptiste Yunès Jan 18 '15 at 17:25
  • You shouldn't be using that event in that class. Use it in your window's class. – Kendel Jan 18 '15 at 19:41
  • @Kendel Sorry, but if I only have AppDelegate, no windows class (it's a menu bar app), where can I put the override func? – Vincent Sep 12 '18 at 13:16

3 Answers3

25

Here is some example code:

  override func keyDown(theEvent: NSEvent) {
            if (theEvent.keyCode == 1){
           //do whatever when the s key is pressed
        } 

    }

Key codes: enter image description here

Kendel
  • 1,698
  • 2
  • 17
  • 33
  • When I use that example code I get a compiler error saying it cannot override method from superclass. I have imported AppKit. Where would I use the example code above to accept keyboard input. Thanks –  Jan 18 '15 at 19:00
  • I just threw together a test project and added it to the initialize application function. When I push a key it makes the "boing sound" on my mac when key input is not accepted. –  Jan 18 '15 at 19:21
  • I thought you got a compiler error? So what is the issue right now? – Kendel Jan 18 '15 at 19:22
  • I still get the compiler error. It will not run compile with "override". I removed override and then ran it and got those results. –  Jan 18 '15 at 19:26
  • Can you edit the OP with the code you are using. The full class please. – Kendel Jan 18 '15 at 19:27
  • You have put the keyDown method inside the applicationDidFinishLaunching method and it should be outside that method. – Fred Appelman Jan 21 '17 at 19:22
  • Put `override func keyDown` into `ViewController.swift` work for me. I am building a macOS app using Swift 5. I tried `Magnet` and `HotKey` lib using Carthage. they work. just document is bad. – NamNamNam May 14 '19 at 09:50
  • thank you so much for posting that picture. I've had no luck sending control and command keys to certain apps. I've been using CGEvent.flags to pass those modifiers. it never occurred to me until I saw that picture that I could be sending the modifiers by themselves (i.e. 59, 55, etc) before and after the keys the modify. – dldnh Apr 01 '21 at 01:02
5
 override func keyDown(with event: NSEvent) {
    if (event.keyCode == 1){
        //do whatever when the s key is pressed
        print("S key pressed")
    }
}

Updated for SWIFT 3

cleme001
  • 341
  • 5
  • 15
2

SWIFT 4: I created a repo solving this problem on my games. I mainly use this with my MTKView but should work with NSViews too.

https://github.com/twohyjr/NSView-Keyboard-and-Mouse-Input

twohyjr
  • 120
  • 9
  • This is excellent - its it possible to use this to intercept keyboard input and filter what is sent on to textfields in a ViewController ? – Ian33 Dec 17 '19 at 11:16