2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Cocoaで複数の修飾キーを認識する

Last updated at Posted at 2018-07-20

メモ程度の内容ですが・・・

最近macOSアプリ開発でCocoaを触ることがあるのでハマったところとか気づいたところを書いておきます。

環境

Xcode 9.4.1
Swift4
Deployment Target 10.13

内容

Cocoaでは flagsChanged() で修飾キー(ShiftとかAltとかCommand)が押された状態を取得できますが、複数キーが押された場合どうすればその状態を確認できるのだろうと少しハマりました。

解決方法

2018/07/21 修正

コメントいただきましたが、modifierFlagsがOptionSetだったので旧方法より短く書くことができます。

if event.modifierFlags.contains([.shift, .command]) {
    // 処理
}

旧方法

なんのことはない、複数キーをANDでとればいいだけでした。

if event.modifierFlags.contains(NSEvent.ModifierFlags.shift) && event.modifierFlags.contains(NSEvent.ModifierFlags.command) {
    // 処理
}
2
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?