macOS/iOS API解説

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。2010年代に書かれた内容です。今後更新はありません。

目次

keyEquivalentModifierMask

INDEX>AppKit>NSButton

ボタンのモディファイアキー返します

Objective-C

@property NSUInteger keyEquivalentModifierMask

Swift

var keyEquivalentModifierMask: Int

解説

ボタンのモディファイアキー返します。
【ビットマスク】
● NSControlKeyMask  コントロールキー
● NSAlternateKeyMask オプション(alt)キー
● NSCommandKeyMask コマンドキー
● NSAlphaShiftKeyMask
● NSShiftKeyMask
● NSNumericPadKeyMask
● NSHelpKeyMask
● NSFunctionKeyMask
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
● NSDeviceIndependentModifierFlagsMask

Swift

theButton.keyEquivalentModifierMask = Int(NSEventModifierFlags.CommandKeyMask.rawValue | NSEventModifierFlags.ShiftKeyMask.rawValue)



返り値

Objective-C

( NSUInteger )

Swift


整数値

フレームワーク

ApplicationKit

クラス

NSButton

使用可能

10.0

参照


keyEquivalent - Cocoa API解説(iOS/OS X)


ビットマスクの判定法[Swift] - Cocoa API解説(iOS/OS X)

関連記事(外部リンク)

keyEquivalentModifierMask problems with Shift | Cocoabuilder

http://stackoverflow.com/questions/22571162/assign-keyboard-shortcut-to-multiple-menu-items



Setting a Menu Item’s Key Equivalent

例文

Objective-C

#import "MyObject.h"

@implementation MyObject

- (IBAction)myAction:(id)sender
{
	[sender setTitle:@"タイトル"];
	[sender setKeyEquivalent:@"m"];
	[sender setKeyEquivalentModifierMask:NSCommandKeyMask];
	
	switch ([sender keyEquivalentModifierMask]) {
		case NSControlKeyMask:
			NSLog(@"NSControlKeyMask");
			break;
		case NSAlternateKeyMask:
			NSLog(@"NSAlternateKeyMask");
			break;
		case NSCommandKeyMask:
			NSLog(@"NSCommandKeyMask");
			break;
		default:
			NSLog(@"other");
			break;
	}
	
	NSBeep();
}

@end

Swift

    //NSButton keyEquivalent
    //NSButton keyEquivalentModifierMask
    @IBAction func function018(sender: AnyObject) {
        var aWindow : NSWindow
        = NSWindow(contentRect: NSMakeRect(0.0, 0.0, 300.0, 200.0),
            styleMask: NSTitledWindowMask
                | NSClosableWindowMask
                | NSMiniaturizableWindowMask
                | NSResizableWindowMask,
            backing: .Buffered,
            defer: false,
            screen: NSScreen.mainScreen())
        windowArray.addObject(aWindow) //ウインドウを保持するための配列に追加。アプリ終了時に配列は破棄
        aWindow.center()//ウインドウをスクリーンの中心に
        aWindow.title = "ウインドウタイトル"//タイトル設定
        //
        var theButton : NSButton = NSButton(frame: NSMakeRect(50.0, 50.0, 100.0, 30.0))
        theButton.title = "Change"
        theButton.bezelStyle = NSBezelStyle.RoundedBezelStyle
        theButton.action = Selector("buttonAction002:")
        theButton.target = self
        theButton.setButtonType(.MomentaryLightButton)
        //コマンド+シフト+「l」でボタンを押したことにする
        theButton.keyEquivalent = "l"
        theButton.keyEquivalentModifierMask = Int(NSEventModifierFlags.CommandKeyMask.rawValue | NSEventModifierFlags.ShiftKeyMask.rawValue)
                //.allZeros
        
        aWindow.contentView.addSubview(theButton)
        
        aWindow.orderFront(self)//前面に
        aWindow.makeKeyAndOrderFront(self)//表示
    }

編集時のバージョン

10.10