Try using keyboard
module (allows us to assign custom shortcut keys):
import pyttsx3
import datetime
import keyboard
def time():
engine = pyttsx3.init()
hours = int(datetime.datetime.now().time().hour)%12
if not hours:
hours = 12
engine.say("the time is " + str(hours) + " " + str(datetime.datetime.now().time().minute))
engine.runAndWait()
def keyPress():
keyboard.add_hotkey('+', lambda: time())
keyboard.wait()
keyPress()
WHAT THIS PROGRAM DOES:-
This program a first creates a custom shortcut (assigns it to keyboard key +
(change this to whatever hotkey combination you want)). Upon pressing this key, a function called time()
gets called, which makes use of windows TTS, to tell your the current time (via voice functionality like siri, alisa etc).
HOW DOES THIS WORK:-
I made use of external libraries keyboard
(necessary) and pyttsx3
(optional), where the module keyboard allows us to define our custom hotkeys and create a event handler for keyboard events, and pyttsx3 makes use of windows Text to Speech feature to read some text via speech (aka voice). I made use of voice functionality, to not interrupt your flow while your are watching something online.
HOW TO SET IT UP:-
- Copy the code, and save it as a
.pyw
file (notice the w in the
end). The difference between a regular .py
an a .pyw
file is
that, .py
invokes console (~commandline) equivalent tool for
execution and executes in foreground, on the other hand a .pyw
file runs in the background at all times (i.e the console won't show
up). So it won't require you to manually open the console at all
times for executing the script as the script will be running in the
background at all time.
- Copy this file (
example.pyw
) in the startup folder of your OS (any
file in this folder will automatically execute once the operating
system has started). What this will do is when your OS, will boot
your example.pyw
file will automatically start executing in the
background, so you won't have to manually launch it at every system
startup. And since it is in the background it won't interfere with
your work. (if you're on windows OS, you can access your startup folder by typing shell:common startup
on your run and pressing enter) (adding files to this folder requires root privileges)
HOW TO USE IT:-
While using your system press +
(for my case), and your OS will tell you what the current time is. (though it requires your OS to have correct time)
PROS:-
Upon using this for long time I can tell you I never encountered a
single application in which the custom defined hotkey won't work.
Slim to None time required after keypress, for command execution.
- A lot resistant to spam of hotkey's. e.x. You may press the
hotkey 100 times but the calls won't be made unless the previous
command has completed execution.
CONS:-
- Calling the script for the first time after Bootup, may cause a
little latency in command execution (but only for once, i.e
subsequent calls would be really fast)
P.S:-
It is optional to use pyttsx3
, I used it as it makes checking time a lot more easier then reading text or something (at least for me).
Secondly, this process could be made more efficient if you want.
RECOMMENDATIONS:-
Alternatively, If you are familiar with AHK (AutoHotKey) then, doing what i just told on a .ahk
script would be a piece of cake, and it's scripts are even faster than my Python one. (Honestly I would definitely Recommend you to use AutoHotKey as it is a really robust language, when it comes to make efficient Operating System scripts)