INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Use media keys except volume up/down  (Read 3101 times)

89ZEF

  • Recent member
  • *
  • Posts: 40
Use media keys except volume up/down
« on: March 24, 2019, 12:50:22 pm »

Topic related to this one describing my issue :
https://yabb.jriver.com/interact/index.php/topic,119955.0.html

Workaround for OSX, using MC Web Server + Hammerspoon
http://www.hammerspoon.org/

- Media Key Mode: disabled
- Volume mode: internal
- Activate Media Network
- Go to Apple > System Preferences > Keyboard
     - Keyboard tab ==> Use Fn keys as standard function keys
     - Shortcuts tab ==> Change Mission Control Shotcut (F11 to something else, I used cmd+F11)

- go to the following URL and save the Token value somewhere
Code: [Select]
http://localhost:52199/MCWS/v1/Authenticate
- Use the following Lua script in Hammerspoon (replace the token value @ first line)
https://pastebin.com/6DBEgdUA

Code: [Select]
token = "PASTE TOKEN HERE"
mcIcon = "/Applications/Media Center 24.app/Contents/Resources/AppIcon.icns"
trackKey = nil

function systemKey(key)
hs.eventtap.event.newSystemKeyEvent(string.upper(key), true):post()
hs.eventtap.event.newSystemKeyEvent(string.upper(key), false):post()
end

function xmlParse(data,item)
result = data:match("<Item Name=\""..item.."\">(.-)</Item>")

if result == nil then
result = ""
else
string.format(result)
end

return result
end

function mcNotify()
hs.timer.doAfter(
1,
function()
hs.http.asyncGet("http://localhost:52199/MCWS/v1/Playback/Info?Token="..token.."", {}, function(code,body,headers)
if code == 200 then
if trackKey ~= xmlParse(body, "FileKey") then
trackKey = xmlParse(body, "FileKey")
notif = hs.notify.new({title=xmlParse(body, "Artist"), informativeText=xmlParse(body, "Name")})
notif:contentImage(hs.image.imageFromURL("http://localhost:52199/"..xmlParse(body, "ImageURL").."&Token="..token))
notif:setIdImage(mcIcon)
notif:send()
end
end
end)
end
)
end

hs.hotkey.bind({}, "f1", function()
systemKey("BRIGHTNESS_DOWN")
end, nil, function()
systemKey("BRIGHTNESS_DOWN")
end)

hs.hotkey.bind({}, "f2", function()
systemKey("BRIGHTNESS_UP")
end, nil, function()
systemKey("BRIGHTNESS_UP")
end)

hs.hotkey.bind({}, "f5", function()
systemKey("ILLUMINATION_DOWN")
end, nil, function()
systemKey("ILLUMINATION_DOWN")
end)

hs.hotkey.bind({}, "f6", function()
systemKey("ILLUMINATION_UP")
end, nil, function()
systemKey("ILLUMINATION_UP")
end)

hs.hotkey.bind({}, "f7", function()
hs.http.get("http://localhost:52199/MCWS/v1/Playback/Previous?Token="..token)
mcNotify()
end)

hs.hotkey.bind({}, "f8", function()
hs.http.get("http://localhost:52199/MCWS/v1/Playback/PlayPause?Token="..token)
end)

hs.hotkey.bind({}, "f9", function()
hs.http.get("http://localhost:52199/MCWS/v1/Playback/Next?Token="..token)
mcNotify()
end)

hs.hotkey.bind({}, "f10", function()
systemKey("MUTE")
end)

hs.hotkey.bind({}, "f11", function()
systemKey("SOUND_DOWN")
end, nil, function()
systemKey("SOUND_DOWN")
end)

hs.hotkey.bind({}, "f12", function()
systemKey("SOUND_UP")
end, nil, function()
systemKey("SOUND_UP")
end)


and voila!

some details as a memo:
- Switching to function keys forces us to rewrite the behaviour of special keys like brightness up/down, all of them are implemented except F3 and F4
- A notification will pop when pressing Previous or Next. You can disable it by commenting lines 67 and 76
Logged

89ZEF

  • Recent member
  • *
  • Posts: 40
Re: Use media keys except volume up/down
« Reply #1 on: March 25, 2019, 01:28:52 am »

new version

- improved notification system: watcher for track changes. notifications pops even no key is pressed.
- efficiency: watcher for Media Center status.

Code: [Select]
token = "TOKEN"
mcIcon = "/Applications/Media Center 24.app/Contents/Resources/AppIcon.icns"
trackKey = 0
mcIsOn = false
mcNotifyLoopSeconds = 5

function systemKey(key)
hs.eventtap.event.newSystemKeyEvent(string.upper(key), true):post()
hs.eventtap.event.newSystemKeyEvent(string.upper(key), false):post()
end

function xmlParse(data,item)
result = data:match("<Item Name=\""..item.."\">(.-)</Item>")
if result == nil then result = "" else string.format(result) end
return result
end

function mcNotify()
hs.timer.doAfter(
1,
function()
hs.http.asyncGet("http://localhost:52199/MCWS/v1/Playback/Info?Token="..token.."", {}, function(code,body,headers)
if (code == 200 and trackKey ~= xmlParse(body, "FileKey")) then
trackKey = xmlParse(body, "FileKey")
notif = hs.notify.new({title=xmlParse(body, "Name"), informativeText=xmlParse(body, "Artist")})
notif:contentImage(hs.image.imageFromURL("http://localhost:52199/"..xmlParse(body, "ImageURL").."&Token="..token))
notif:setIdImage(mcIcon)
notif:send()
end
end)
end
)
end

function mcNotifyLoop()
if mcIsOn == true then mcNotify() end
end
hs.timer.doEvery(mcNotifyLoopSeconds, mcNotifyLoop)

hs.application.watcher.new(function(name, event, app)
if name == "Media Center 24" then
if event == 1 then mcIsOn = true elseif event == 2 then mcIsOn = false end
end
end):start()

-- Keyboard Shortcuts

hs.hotkey.bind({}, "f1", function() systemKey("BRIGHTNESS_DOWN") end, nil, function() systemKey("BRIGHTNESS_DOWN") end)
hs.hotkey.bind({}, "f2", function() systemKey("BRIGHTNESS_UP") end, nil, function() systemKey("BRIGHTNESS_UP") end)
hs.hotkey.bind({}, "f5", function() systemKey("ILLUMINATION_DOWN") end, nil, function() systemKey("ILLUMINATION_DOWN") end)
hs.hotkey.bind({}, "f6", function() systemKey("ILLUMINATION_UP") end, nil, function() systemKey("ILLUMINATION_UP") end)
hs.hotkey.bind({}, "f10", function() systemKey("MUTE") end)
hs.hotkey.bind({}, "f11", function() systemKey("SOUND_DOWN") end, nil, function() systemKey("SOUND_DOWN") end)
hs.hotkey.bind({}, "f12", function() systemKey("SOUND_UP") end, nil, function() systemKey("SOUND_UP") end)

-- Play/Pause
hs.hotkey.bind({}, "f8", function()
if mcIsOn == true then hs.http.get("http://localhost:52199/MCWS/v1/Playback/PlayPause?Token="..token) end
end)

-- Previous
hs.hotkey.bind({}, "f7", function()
if mcIsOn == true then
hs.http.get("http://localhost:52199/MCWS/v1/Playback/Previous?Token="..token)
mcNotify()
end
end)

-- Next
hs.hotkey.bind({}, "f9", function()
if mcIsOn == true then
hs.http.get("http://localhost:52199/MCWS/v1/Playback/Next?Token="..token)
mcNotify()
end
end)
Logged
Pages: [1]   Go Up