我正在尝试使用PyObjC创建一个应用程序,我正在努力寻找如何记录箭头键(左和右)。我希望每次用户按下左右箭头键时都能记录。我正在使用在线发现的另一个例子。我想使用键盘上的箭头键,而不是前面例子中用于增量和损坏的按钮。看了一会儿,以为我可以在这里得到一些帮助。谢谢!
from Cocoa import *
from Foundation import NSObject
class TAC_UI_Controller(NSWindowController):
counterTextField = objc.IBOutlet()
def windowDidLoad(self):
NSWindowController.windowDidLoad(self)
# Start the counter
self.count = 0
@objc.IBAction
def increment_(self, sender):
self.count += 1
self.updateDisplay()
@objc.IBAction
def decrement_(self, sender):
self.count -= 1
self.updateDisplay()
def updateDisplay(self):
self.counterTextField.setStringValue_(self.count)
if __name__ == "__main__":
app = NSApplication.sharedApplication()
# Initiate the contrller with a XIB
viewController = test.alloc().initWithWindowNibName_("test")
# Show the window
viewController.showWindow_(viewController)
# Bring app to top
NSApp.activateIgnoringOtherApps_(True)
from PyObjCTools import AppHelper
AppHelper.runEventLoop()
答案 0 :(得分:0)
您的NSView
派生类应该实现keyDown_
和/或keyUp_
。您还需要acceptsFirstResponder
返回True:
from AppKit import NSView
class MyView(NSView)
def keyDown_(self, event):
pass
def keyUp_(self, event):
pass
def acceptsFirstResponder(self):
return True
这里是您可以使用的PyObjC文档的示例实现:https://pythonhosted.org/pyobjc/examples/Cocoa/AppKit/DragItemAround/index.html