Skip to content

Commit

Permalink
[New Extension] Media Key Emulate (raycast#7394)
Browse files Browse the repository at this point in the history
* global-media-key: init

* tweak: resolved problem

* build: update deps

* Fix typo

---------

Co-authored-by: Milena Araujo <mil3na@users.noreply.github.com>
  • Loading branch information
douo and mil3na authored Jul 17, 2023
1 parent 2f37f17 commit 62537b4
Show file tree
Hide file tree
Showing 25 changed files with 2,062 additions and 0 deletions.
4 changes: 4 additions & 0 deletions extensions/global-media-key/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"root": true,
"extends": ["@raycast"]
}
13 changes: 13 additions & 0 deletions extensions/global-media-key/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules

# misc
.DS_Store

# swiftc
.build

# https://developers.raycast.com/migration/v1.50.0
raycast-env.d.ts
4 changes: 4 additions & 0 deletions extensions/global-media-key/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"printWidth": 120,
"singleQuote": false
}
3 changes: 3 additions & 0 deletions extensions/global-media-key/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Media Key Emulate Changelog

## [Initial Version] - 2023-03-20
21 changes: 21 additions & 0 deletions extensions/global-media-key/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Tiou Lims

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
15 changes: 15 additions & 0 deletions extensions/global-media-key/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// swift-tools-version: 5.6
import PackageDescription

let package = Package(
name: "media-key",
platforms: [
.macOS(.v10_15),
],
targets: [
.executableTarget(
name: "media-key",
dependencies: []
),
]
)
3 changes: 3 additions & 0 deletions extensions/global-media-key/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Media Key Emulate

Emulate keyboard media keys press, reassign hotkey for media keys.
62 changes: 62 additions & 0 deletions extensions/global-media-key/Sources/media-key/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import Quartz
import Cocoa
import Carbon
import ApplicationServices


func isProcessTrusted() -> Bool {
let options = [kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String: false]
return AXIsProcessTrustedWithOptions(options as CFDictionary?)
}

// https://opensource.apple.com/source/IOHIDFamily/IOHIDFamily-34.12/IOHIDSystem/IOKit/hidsystem/ev_keymap.h.auto.html
let dict = [
"sound_up": NX_KEYTYPE_SOUND_UP,
"sound_down": NX_KEYTYPE_SOUND_DOWN,
"play": NX_KEYTYPE_PLAY,
"next": NX_KEYTYPE_NEXT,
"previous": NX_KEYTYPE_PREVIOUS,
"fast": NX_KEYTYPE_FAST,
"rewind": NX_KEYTYPE_REWIND,
]

// help from https://stackoverflow.com/a/55854051
func HIDPostAuxKey(key: Int32) {
func doKey(down: Bool) {
let flags = NSEvent.ModifierFlags(rawValue: (down ? 0xa00 : 0xb00))
let data1 = Int((key<<16) | (down ? 0xa00 : 0xb00))

let ev = NSEvent.otherEvent(with: NSEvent.EventType.systemDefined,
location: NSPoint(x:0,y:0),
modifierFlags: flags,
timestamp: 0,
windowNumber: 0,
context: nil,
subtype: 8,
data1: data1,
data2: -1
)
let cev = ev?.cgEvent
cev?.post(tap: CGEventTapLocation.cghidEventTap)

}
doKey(down: true)
// https://stackoverflow.com/a/75084671
usleep(useconds_t(1 * 1_000)) //will sleep for 1 millisecond (.001 seconds
doKey(down: false)
}

let args = CommandLine.arguments
let keyType = args[args.count - 1]
if let raw = dict[keyType]{
print("Trusted: \(AXIsProcessTrusted())")
if isProcessTrusted() {
HIDPostAuxKey(key:raw)
} else {
print("This process is not authorized for assistive access")
exit(1)
}

}else{
exit(1)
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added extensions/global-media-key/assets/fast.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added extensions/global-media-key/assets/media-key
Binary file not shown.
Binary file added extensions/global-media-key/assets/next.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added extensions/global-media-key/assets/play.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added extensions/global-media-key/assets/previous.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added extensions/global-media-key/assets/rewind.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 62537b4

Please sign in to comment.