20211026のMacに関する記事は3件です。

【Swift】Macでメディアキーの押下をエミュレートする

メディアキーとは ここでメディアキーと呼んでいるのは、キーボードの最上段にある↓このキーのことです。 メディアキーをプログラムから入力するコードは以下の通り。 メディアキー入力 func HIDPostMediaKey(_ key: Int32) { func doMediaKey(_ key: Int, down: Bool) { let modifierFlags = NSEvent.ModifierFlags(rawValue: down ? 0xA00 : 0xB00) let nsEvent = NSEvent.otherEvent(with: .systemDefined, location: NSPoint(x: 0, y: 0), modifierFlags: modifierFlags, timestamp: 0, windowNumber: 0, context: nil, subtype: 8, data1: (key << 16) | ((down ? 0xA : 0xB) << 8), data2: -1) let cgEvent = nsEvent?.cgEvent cgEvent?.post(tap: .cghidEventTap) } doMediaKey(Int(key), down: true) //key press doMediaKey(Int(key), down: false) //key release } 使い方は簡単で、引数にキータイプを指定するだけ。例えば、音量アップならNX_KEYTYPE_SOUND_UPを指定する。 音量アップ HIDPostMediaKey(NX_KEYTYPE_SOUND_UP) 引数で指定するキータイプは、下記の通り。 キー キータイプ定数 NX_KEYTYPE_BRIGHTNESS_DOWN NX_KEYTYPE_BRIGHTNESS_UP NX_KEYTYPE_REWIND NX_KEYTYPE_PLAY NX_KEYTYPE_FAST NX_KEYTYPE_MUTE NX_KEYTYPE_SOUND_DOWN NX_KEYTYPE_SOUND_UP NX_KEYTYPE_EJECT 上のものも含めて、キータイプは下記の通り記述されている。いくつか試したが、機能しているかどうか確認できなかったものが多かった。 IOKit.hidsystem public var NX_NOSPECIALKEY: Int32 { get } public var NX_KEYTYPE_SOUND_UP: Int32 { get } public var NX_KEYTYPE_SOUND_DOWN: Int32 { get } public var NX_KEYTYPE_BRIGHTNESS_UP: Int32 { get } public var NX_KEYTYPE_BRIGHTNESS_DOWN: Int32 { get } public var NX_KEYTYPE_CAPS_LOCK: Int32 { get } public var NX_KEYTYPE_HELP: Int32 { get } public var NX_POWER_KEY: Int32 { get } public var NX_KEYTYPE_MUTE: Int32 { get } public var NX_UP_ARROW_KEY: Int32 { get } public var NX_DOWN_ARROW_KEY: Int32 { get } public var NX_KEYTYPE_NUM_LOCK: Int32 { get } public var NX_KEYTYPE_CONTRAST_UP: Int32 { get } public var NX_KEYTYPE_CONTRAST_DOWN: Int32 { get } public var NX_KEYTYPE_LAUNCH_PANEL: Int32 { get } public var NX_KEYTYPE_EJECT: Int32 { get } public var NX_KEYTYPE_VIDMIRROR: Int32 { get } public var NX_KEYTYPE_PLAY: Int32 { get } public var NX_KEYTYPE_NEXT: Int32 { get } public var NX_KEYTYPE_PREVIOUS: Int32 { get } public var NX_KEYTYPE_FAST: Int32 { get } public var NX_KEYTYPE_REWIND: Int32 { get } public var NX_KEYTYPE_ILLUMINATION_UP: Int32 { get } public var NX_KEYTYPE_ILLUMINATION_DOWN: Int32 { get } public var NX_KEYTYPE_ILLUMINATION_TOGGLE: Int32 { get } このファイルの定数をSwiftで宣言しただけと思われる。(NeXTが懐かしい) なお、上記のコードをはじめて実行すると、システム環境設定のプライバシーが開くので、該当appにアクセシビリティの制御許可を与える必要があります。 インテルMacbook Pro / macOS 11.6 / Xcode 13.1 / Swift 5.5.1 で動作確認済み 参考 以下のPythonコードを参考にした。 キーボードとマウスのエミュレート方法は↓この記事にあります。
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

Using Apple Magic Keyboard on Windows PC

Introduction Maybe you want to use the Apple Magic Keyboard on your Windows PC as well. I took a try. It works fine, and the shortcut "CTRL+ALT+DELETE" can also work after key remapping. Setup is very simple. (No Boot Camp driver installation required.) Validation Environment: Apple Magic Keyboard - English (US) OS: Windows 10 Connect with Bluetooth Handle Special Keys Connect with Bluetooth Don't forget switch on your Magic Keyboard (green becomes visible)。 "Settings" -> "Devices" -> "Bluetooth & other devices" -> "Add bluetooth or other device" Click "Bluetooth" to add your Magic keyboard. Click "Connect" button to start the connection. The connection is complete when you get the following message. After connecting, you can use the keyboard immediately. No driver installation is required. Handle Special Keys DELETE Key As you konw, the "delete" key on the Magic keyboard is equivalent to the "Backspace" key on Windows PC. If you want to delete forwards on Mac, you need to type "fn+delete". Unfortunately, "fn+delete" doesn't work on Windows PC. When you want to delete forward, "shift+delete" will work. (No key remapping is required.) CTRL+ALT+DELETE Key The "option" key on the Magic keyboard is equivalent to ALT, but when I type "control+option+delete", there is no response. "control+option+shift+delete" doesn't work either. So how can I type "CTRL+ALT+DELETE" ? With the help of following software and key remapping, this issue can be solved. Name: Microsoft PowerToys (Product web page) Supported OS: Windows 10 v1903 and later Click here for installation. Keyboard Manager Startup PowerToys and select "Keyboard Manager". Remap Key This example remaps "F12" to "Delete". (Choose your favorite key.) Click "Type" to show the key input screen. Type "F12" and click the OK button. Select "Delete" from the list under "Mapped To", then click "OK" button to remap. Continue anyway when you get the following message. After remapping: Now the key remapping is complete. If you type "control+option+F12", the operation will be the same as "CTRL+ALT+DELETE". ※Please note that Keyboard Manager CAN NOT remap shortcut "CTRL+ALT+DELETE" directly. End Reference Keyboard Manager utility Related Blog Magicキーボードは、Windows PCも使える (Japanese version)
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む

MacのUSキーボードで日本語環境を快適に! MacもWinも同じ⌘キーで!

Karabiner-elementsを利用したキー割り当て設定:MacOS/Windows10onVirtualBox MacユーザでUSキーボードを使いたい人は多い、またVirtualBoxでWindows環境を併用している人も多い、私もその一人である。 しばらくはMacキーバインドとWindowsキーバインドでそれぞれ使っていたが、やはり同じキー操作で使うのが快適であると思い立ち、Karabiner-elementsを利用することで「macもwinも同じキー操作に」する方法を紹介する。 確認した環境情報:Apple純正USキーボード(Magic Keyboard US)で... MacOS BigSur、Karabiner-elements 13.7、Windows10 on VirtualBox 6.1 実現したことは下記: (1)MacOSでの日本語切替は、左⌘キーを押すと「英字」に、右⌘キーを押すと「ひらがな」になる   (↑これまで通り、⌘+c, ⌘+v, ⌘+x, ⌘+z, ⌘+a, ⌘+s ...などは問題なく機能する) (2)Windows10 onVirtualBoxでは、左⌘キーを押すと「英数」に、右⌘キーを押すと「かな」になる   (↑Macと同じ様に、⌘+c, ⌘+v, ⌘+x, ⌘+z, ⌘+a, ⌘+s ...なども機能する) (3)おまけ:Windows10 onVirtualBoxで、⌘と←, →, ↑, ↓ の押下で それぞれ 行top, 行end, 文書top, 文書end おまけのカーソル移動:注意点 ・MacOSのMS-Officeは一般的なMacキー所作とは異なる部分あり(⌘やctrlの組合せに関して) ・MacOSではctrl+aやctrl+eでも行端移動できるが今回方法のWindows上では未対応 (1)MacOSでUSキーボードの左右の⌘で日本語のON/OFFする手順 まず、Karabiner-elements をインストールする。 インストールしたら、[Preferences]メニューから表示されるポップアップ画面の[Complex modifications]タブを選んで、左下のボタン[Add rule]を押す。 すると、ポップアップに[Import more rules from the Internet]というボタンが現れるのでコレを押してブラウザを開く。 ブラウザ表示されたページの上部リンクから[International(Language Specific)]をクリックすると、ページ下方にジャンプするので[For Japanese(日本語環境向けの設定)] を"Import"する。 Karabiner-elementsにImportされたので、[Preferences]メニューの一覧から「コマンドキーを単体で押したときに、英数・かなキーを送信する」を"Enable"にする。 これで、左⌘キーを押すと英字モード、右⌘キーを押すとひらがなモードの切り替えが実現する。 (2)Windows10(VirtualBox)で、MacOSと同じに左右の⌘で日本語のON/OFFする手順 Karabiner-elementsの既存rulesに望みのものが見つからなかったので、新たにruleを作成(JSON記述) 下記のコードでJSONファイルを作成します。 (※下記コードには、(3)のおまけ:カーソル移動キーruleも含んでいます) JapaneseSettingForMacUSkeyOnVirtualBox.json { "title": "Japanese-Setting for US-keyboard on VirtualBox", "rules": [ { "description": "Japanese On/Off : Make ⌘key for Japanese on(right⌘)/off(left⌘) as 'CTRL+]'/'CTRL+[' under VirtualBox", "manipulators": [ { "type": "basic", "from": { "key_code": "left_command", "modifiers": { "optional": [ "any" ] } }, "parameters": { "basic.to_if_held_down_threshold_milliseconds": 100 }, "to": [ { "key_code": "left_control", "lazy": true } ], "to_if_held_down": [ { "key_code": "left_control" } ], "to_if_alone": [ { "key_code": "open_bracket" , "modifiers" : [ "left_control" ] } ], "conditions": [ { "type": "frontmost_application_if", "bundle_identifiers": [ "^org\\.virtualbox\\.app\\.VirtualBoxVM$" ] } ] }, { "type": "basic", "from": { "key_code": "right_command", "modifiers": { "optional": [ "any" ] } }, "parameters": { "basic.to_if_held_down_threshold_milliseconds": 100 }, "to": [ { "key_code": "left_control", "lazy": true } ], "to_if_held_down": [ { "key_code": "left_control" } ], "to_if_alone": [ { "key_code": "close_bracket" , "modifiers" : [ "left_control" ] } ], "conditions": [ { "type": "frontmost_application_if", "bundle_identifiers": [ "^org\\.virtualbox\\.app\\.VirtualBoxVM$" ] } ] } ] }, { "description": "Cursor-Move with CTRL+←,→,↑,↓ (i.e. CTRL=⌘) as lineTop,lineEnd,docTop,docEnd under VirtualBox", "manipulators": [ { "type": "basic", "from": { "key_code": "left_arrow", "modifiers": { "mandatory":["left_control"] } }, "to": [ { "key_code": "left_arrow", "modifiers": [ "fn" ] } ], "conditions": [ { "type": "frontmost_application_if", "bundle_identifiers": [ "^org\\.virtualbox\\.app\\.VirtualBoxVM$" ] } ] }, { "type": "basic", "from": { "key_code": "right_arrow", "modifiers": { "mandatory":["left_control"] } }, "to": [ { "key_code": "right_arrow", "modifiers": [ "fn" ] } ], "conditions": [ { "type": "frontmost_application_if", "bundle_identifiers": [ "^org\\.virtualbox\\.app\\.VirtualBoxVM$" ] } ] }, { "type": "basic", "from": { "key_code": "up_arrow", "modifiers": { "mandatory":["left_control"] } }, "to": [ { "key_code": "home", "modifiers": [ "left_control" ] } ], "conditions": [ { "type": "frontmost_application_if", "bundle_identifiers": [ "^org\\.virtualbox\\.app\\.VirtualBoxVM$" ] } ] }, { "type": "basic", "from": { "key_code": "down_arrow", "modifiers": { "mandatory":["left_control"] } }, "to": [ { "key_code": "end", "modifiers": [ "left_control" ] } ], "conditions": [ { "type": "frontmost_application_if", "bundle_identifiers": [ "^org\\.virtualbox\\.app\\.VirtualBoxVM$" ] } ] } ] } ] } このruleの概要:VirtualBox環境の時のみに適用されるルール ・単独で左⌘キーを押した場合にctrl+[ に置換え、右⌘キーを押した場合にctrl+] に置換える  (↑後でWindows側のIME設定変更あり:ctrl+[ , ctrl+] を 英数, かな に変更) ・⌘を装飾キー(組合せ)で使う場合は、ctrlとして機能する ・カーソル移動に⌘を使えるようにする:⌘+←,→,↑,↓ は 行頭, 行末, 文書頭, 文書末へ移動する  (↑⌘はctrlに置き換わっているので、実際はctrl+←,→,↑,↓ の置換え処理) 作成したJSONファイルを、Karabiner-elementsのrule保存場所(下記)に保存します。 rule格納場所 ~/.config/karabiner/assets/complex_modifications Karabiner-elementsの[Preferences]メニューから表示されるポップアップ画面の[Complex modifications]タブを選んで、左下のボタン[Add rule]を押す。 表示されるruleから、"Japanese-Setting for US-keyboard on VirtualBox"の [Japanese On/Off:...] を"Enable"にする。 【重要】EnableになったRulesの順番を変更します、[Japanese On/Off:...]を "↑Up"ボタン で先頭に移動します。 【重要】ここで、VirtualBoxを起動してWindows10のVMにし、IMEのキー割り当てを変更します。 IMEの「あ/A アイコン」で右クリックしてプロパティを開きます、さらに詳細設定を開いて、[全般][編集操作][キー設定]の"変更"ボタンから開いたウインドウで"キー追加"ボタンを押し、ctrl+[ を押下でOKします。これで追加されたctrl+[の行の先頭項目'入力/変換済み文字なし'を"IME-オフ"に設定します(項目を右クリック) 同じように、"キー追加"ボタンを押し、ctrl+] を押下でOKします。これで追加されたctrl+]の行の先頭項目'入力/変換済み文字なし'を"IME-オン"に設定します(項目を右クリック) ※つまり、Karabiner-elementsで、左⌘はctrl-[ , 右⌘はctrl-] となっているので、これをIME-オフ, オンにする これで、Windows10(VirtualBox)で、Macと同じように左⌘キー/右⌘キーで日本語が切り替わります。 ★追記!:困ったWindows変更:IMEプロパティ設定が勝手に無くなる!!キー変更が機能しない... 対処方法:いろんな人が文句を言いながらも対処を載せている(下記参考:記事の一例) ※一度プロパティ設定してある場合は、プロパティ表示が無くなっても復活させればキー設定も復活します 【参考URL:IMEプロパティ復活】 IMEのプロパティがない!詳細設定がない!最短で元に戻す方法【Windows 10】 (3)おまけ:Windows10(VirtualBox)で、MacOSと同じに⌘+←,→,↑,↓ でカーソル移動の手順 Karabiner-elementsの[Preferences]メニューから表示されるポップアップ画面の[Complex modifications]タブを選んで、左下のボタン[Add rule]を押す。 表示されるruleから、"Japanese-Setting for US-keyboard on VirtualBox"の [CursorMove with CTRL...] を"Enable"にする。 【重要】EnableになったRulesの順番を変更します、[CursorMove with CTRL...]を "↑Up"ボタン で先頭の次(2番目)に移動します。 これで、Windows10(VirtualBox)で、Macと同じように⌘+←,→,↑,↓ が行頭,行末,文書頭,文書末へのカーソル移動となります。 参考情報: 快適キーボード操作のためのキーカスタマイズ ~Mac編~ Karabiner Elementsの記号対応表
  • このエントリーをはてなブックマークに追加
  • Qiitaで続きを読む