|
| 1 | +""" |
| 2 | +Note |
| 3 | +---- |
| 4 | +'import pycaw.magic' must be generally at the topmost. |
| 5 | +To be more specific: |
| 6 | +It needs to be imported before any other pycaw or comtypes import. |
| 7 | +
|
| 8 | +
|
| 9 | +Reserved Atrributes |
| 10 | +------------------- |
| 11 | +Note that certain methods and attributes are reserved for the magic module. |
| 12 | + Please look into the source code of MagicApp for more information. |
| 13 | +But to avoid conflicts now and in the future, i recommend using |
| 14 | +a prefix for each of your custom methods and attributes. |
| 15 | +
|
| 16 | +
|
| 17 | +Features |
| 18 | +-------- |
| 19 | +Instantiate a new MagicApp with one or more app executables: |
| 20 | +
|
| 21 | +magic = MagicApp({"msedge.exe", "another.exe"}) |
| 22 | +
|
| 23 | +-------- |
| 24 | +
|
| 25 | +you could also inherit from MagicApp and create customized callbacks: |
| 26 | +
|
| 27 | +class MyCustomApp(MagicApp): |
| 28 | + def __init__(self, app_execs): |
| 29 | + super().__init__(app_execs, |
| 30 | + volume_callback=self.custom_volume_callback, |
| 31 | + mute_callback=self..., |
| 32 | + state_callback=self..., |
| 33 | + session_callback=self...) |
| 34 | +
|
| 35 | + def custom_volume_callback(self, volume): |
| 36 | + print(volume) |
| 37 | + print(self.mute) |
| 38 | + self.mute = True |
| 39 | + print(self.mute) |
| 40 | +
|
| 41 | +mega_magic = MyCustomApp({"msedge.exe"}) |
| 42 | +""" |
| 43 | + |
| 44 | +import time |
| 45 | +from contextlib import suppress |
| 46 | + |
| 47 | +from pycaw.magic import MagicApp |
| 48 | + |
| 49 | + |
| 50 | +def handle_all(*args): |
| 51 | + print("callback") |
| 52 | + print(args) |
| 53 | + |
| 54 | + |
| 55 | +magic = MagicApp({"msedge.exe"}, |
| 56 | + volume_callback=handle_all, |
| 57 | + mute_callback=handle_all, |
| 58 | + state_callback=handle_all, |
| 59 | + session_callback=handle_all) |
| 60 | + |
| 61 | + |
| 62 | +def main(): |
| 63 | + with suppress(KeyboardInterrupt): |
| 64 | + for _ in range(5): |
| 65 | + """ |
| 66 | + open and close your MagicApp app_exec (msedge.exe) |
| 67 | + and see how it will change the volume as long as |
| 68 | + the app is opened. When you close app_exec it wont change |
| 69 | + the volume and None is printed. |
| 70 | +
|
| 71 | + if you change for example the volume in the Windows sound mixer |
| 72 | + handle_all() is fired. |
| 73 | + """ |
| 74 | + |
| 75 | + if magic.state is None: |
| 76 | + print(f"No session active for: {magic}") |
| 77 | + time.sleep(2) |
| 78 | + continue |
| 79 | + |
| 80 | + print("Volume:") |
| 81 | + magic.volume = 0.1 |
| 82 | + print(magic.volume) |
| 83 | + time.sleep(1) |
| 84 | + magic.volume = 0.9 |
| 85 | + print(magic.volume) |
| 86 | + time.sleep(1) |
| 87 | + |
| 88 | + print(f"{str(magic.state)} {magic.app_execs}") |
| 89 | + |
| 90 | + print("Mute:") |
| 91 | + magic.mute = True |
| 92 | + print(magic.mute) |
| 93 | + time.sleep(1) |
| 94 | + magic.mute = False |
| 95 | + print(magic.mute) |
| 96 | + |
| 97 | + print("\nTschüss") |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == '__main__': |
| 101 | + main() |
0 commit comments