NSButtonに矢印キーを割り当てる [AppleScript辞書はつくれるか?]
NSButtonを配置した場合に、指定をしたキーボードのキーでボタンをクリックをさせることができるのですが、
これをNibから指定してやるのは簡単なのです。
Key Equivalent の項目に、使いたいキーを登録するだけで普通のキーでも特殊なキーでも使えてしまうのですが、
これを、コードだけで指示するのはかなり難題でした。(知ってしまえば簡単なのですがね)
通常のキーであれば
set aKey to "a" --|使用するkey| actionButton1's setKeyEquivalent_(aKey) |
で設定し使用できるようになる。(例では"a"キー)
場合によっては矢印の文字が描かれたキー(arrow key)が使いたくなることがあるのですが、矢印を直接設定すると
set aKey to "↑" --|使用するkey| actionButton1's setKeyEquivalent_(aKey) |
これでは、動いてくれない。結局、"↑"と入力しても、2バイト文字では実際のキーとは認識されないのです。
Nibでは表示してるのに不思議なのだけど、あれは仮の表示なのでしょうね。
ということで、実際に使えるコードを調べてみました。
actionButton1's setKeyEquivalent_(character id (current application's NSUpArrowFunctionKey)) |
めんどくさいので1つにしましたが、NSUpArrowFunctionKeyというのが上矢印Keyに該当するので使用します。単体ではApplescriptで使用できないので current application を
参照させます。
これだけでは、キーの番号しか返ってこないので、Character Id で文字に変更します。これでなんとか使えるようです。
(↑)NSUpArrowFunctionKey
(↓)NSDownArrowFunctionKey
(←)NSLeftArrowFunctionKey
(→)NSRightArrowFunctionKey
script AppDelegate property parent : class "NSObject" property theWindow : missing value property NSWindow : class "NSWindow" property NSButton : class "NSButton" global upArrowKeyButton, downArrowKeyButton, leftArrowKeyButton, rightArrowKeyButton
on applicationWillFinishLaunching_(aNotification) set mainView to theWindow's contentView()
(* make NSButton *) set theRect to current application's NSMakeRect(160,280,120,35) set upArrowKeyButton to NSButton's alloc()'s initWithFrame_(theRect) tell upArrowKeyButton setBezelStyle_(1) setButtonType_(0) setEnabled_(true) setTarget_(me) setTitle_("↑") setAction_("upKeyAction") setKeyEquivalent_(character id (current application's NSUpArrowFunctionKey)) end tell mainView's addSubview_(upArrowKeyButton)
set theRect to current application's NSMakeRect(160,80,120,35) set downArrowKeyButton to NSButton's alloc()'s initWithFrame_(theRect) tell downArrowKeyButton setBezelStyle_(1) setButtonType_(0) setEnabled_(true) setTarget_(me) setTitle_("↓") setAction_("downKeyAction") setKeyEquivalent_(character id (current application's NSDownArrowFunctionKey)) end tell mainView's addSubview_(downArrowKeyButton)
set theRect to current application's NSMakeRect(60,180,120,35) set leftArrowKeyButton to NSButton's alloc()'s initWithFrame_(theRect) tell leftArrowKeyButton setBezelStyle_(1) setButtonType_(0) setEnabled_(true) setTarget_(me) setTitle_("←") setAction_("leftKeyAction") setKeyEquivalent_(character id (current application's NSLeftArrowFunctionKey)) end tell mainView's addSubview_(leftArrowKeyButton)
set theRect to current application's NSMakeRect(260,180,120,35) set rightArrowKeyButton to NSButton's alloc()'s initWithFrame_(theRect) tell rightArrowKeyButton setBezelStyle_(1) setButtonType_(0) setEnabled_(true) setTarget_(me) setTitle_("→") setAction_("rightKeyAction") setKeyEquivalent_(character id (current application's NSRightArrowFunctionKey)) end tell mainView's addSubview_(rightArrowKeyButton)
end applicationWillFinishLaunching_
on upKeyAction() log "「上」矢印キーが押されました" end upKeyAction
on downKeyAction() log "「下」矢印キーが押されました" end downKeyAction
on leftKeyAction() log "「左」矢印キーが押されました" end leftKeyAction
on rightKeyAction() log "「右」矢印キーが押されました" end rightKeyAction
on applicationShouldTerminateAfterLastWindowClosed_(sender) return true end applicationShouldTerminateAfterLastWindowClosed_ on applicationShouldTerminate_(sender) return current application's NSTerminateNow end applicationShouldTerminate_ end script
|
コメント 0