Skip to content

Commit 21857e5

Browse files
authored
Add files via upload
1 parent 7b7e95c commit 21857e5

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

MuteWhenTalk.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""
2+
Title: Dynamic Audio Mute Toggle
3+
Description: This script allows users to mute and unmute their system's audio output dynamically using a specified key.
4+
It leverages the 'pycaw' library for controlling the system's audio and the 'keyboard' library for capturing key presses and releases.
5+
Usage:
6+
1. Run the script in a Python environment where 'keyboard' and 'pycaw' libraries are installed.
7+
2. When prompted, press and release the key you wish to use for muting and unmuting the audio. Keep the key pressed for a moment to set it.
8+
3. Once set, you can press and hold the designated key to mute the audio. Releasing the key will unmute the audio.
9+
4. The script will provide feedback in the console indicating the current audio state ("Audio muted." or "Audio unmuted.") each time the key is pressed or released.
10+
5. To stop the script, use the keyboard interrupt shortcut (usually Ctrl+C in the terminal).
11+
12+
Requirements:
13+
- Python 3.x
14+
- pycaw library: Install via pip with `pip install pycaw`
15+
- keyboard library: Install via pip with `pip install keyboard`
16+
Note: Running scripts that listen to keyboard events may require administrative privileges.
17+
18+
Author: [Your Name]
19+
Date: YYYY-MM-DD
20+
Version: 1.0
21+
22+
Note: This script is designed for educational and practical purposes, allowing users to control their audio settings directly from their keyboard.
23+
The author assumes no responsibility for any misuse or damage caused by this script.
24+
"""
25+
26+
import keyboard
27+
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
28+
from ctypes import cast, POINTER
29+
from comtypes import CLSCTX_ALL
30+
31+
# Initialize the audio controller
32+
devices = AudioUtilities.GetSpeakers()
33+
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
34+
volume = cast(interface, POINTER(IAudioEndpointVolume))
35+
36+
# This flag will help us to mute the audio only once per key press
37+
is_audio_muted = False
38+
39+
# Function to mute audio
40+
def mute_audio():
41+
global is_audio_muted
42+
if not is_audio_muted:
43+
volume.SetMute(1, None)
44+
print("Audio muted.")
45+
is_audio_muted = True
46+
47+
# Function to unmute audio
48+
def unmute_audio():
49+
global is_audio_muted
50+
if is_audio_muted:
51+
volume.SetMute(0, None)
52+
print("Audio unmuted.")
53+
is_audio_muted = False
54+
55+
def detect_and_bind_mute_key():
56+
print("Please press and release the key you want to use for muting (keep it pressed for a moment)...")
57+
58+
event = keyboard.read_event()
59+
if event.event_type == keyboard.KEY_DOWN:
60+
user_key = event.name
61+
# Ensuring the key is released before proceeding (to avoid immediate toggling)
62+
while True:
63+
event = keyboard.read_event()
64+
if event.event_type == keyboard.KEY_UP and event.name == user_key:
65+
break
66+
67+
print(f"The '{user_key}' key has been set as your mute/unmute toggle.")
68+
69+
# Bind the detected key to mute on press and unmute on release
70+
keyboard.on_press_key(user_key, lambda _: mute_audio())
71+
keyboard.on_release_key(user_key, lambda _: unmute_audio())
72+
73+
print(f"Press and hold the '{user_key}' key to mute, and release to unmute.")
74+
return user_key
75+
else:
76+
return None
77+
78+
def main():
79+
user_key = detect_and_bind_mute_key()
80+
if user_key:
81+
# Keep the script running
82+
keyboard.wait()
83+
84+
if __name__ == "__main__":
85+
main()

0 commit comments

Comments
 (0)