Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register media keys in python

I have a mp3 playing program but how do I make it listen for media keys in the background. And would the Fn media keys be different to other dedicated media keys?

like image 347
mrdeadguy34 Avatar asked Sep 15 '25 01:09

mrdeadguy34


1 Answers

from pynput.keyboard import Listener


def on_press(key):
    if str(key) == '<179>':
        # play pause media key was pressed
    if str(key) == '<176>':
        # next key was pressed
    if str(key) == '<177>':
        # previous key was pressed


def on_release(key):
    pass
    
    
listener_thread = Listener(on_press=on_press, on_release=None)
# This is a daemon=True thread, use .join() to prevent code from exiting  
listener_thread.start()

pynput~=1.4.5

Don't use the latest version until this issue is fixed

like image 97
Elijah Avatar answered Sep 16 '25 15:09

Elijah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!