Last active
April 28, 2018 19:41
-
-
Save marczellm/9bb3a39c14fdf5a28c47ff132307aff6 to your computer and use it in GitHub Desktop.
Google Desktop replacement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ctypes | |
import keyboard | |
import tkinter as tk | |
import urllib.parse | |
import webbrowser | |
from threading import Thread | |
class Wnd: | |
def __init__(self, hotkey_state): | |
# Initialize members | |
self.root = tk.Tk() | |
self.hotkey_state = hotkey_state | |
root = self.root | |
self.entry = tk.Entry(root, width=30, font='Helvetica 26') | |
entry = self.entry | |
entry.pack(padx=2, pady=2) | |
# Setup layout | |
root.wm_overrideredirect(True) | |
root.update_idletasks() | |
w = root.winfo_width() | |
h = root.winfo_height() | |
w0 = root.winfo_screenwidth() | |
h0 = root.winfo_screenheight() | |
root.attributes('-topmost', True) | |
root.geometry('%dx%d+%d+%d' % (w, h, (w0-w)/2, (h0-h)/2)) | |
# Setup handlers | |
root.bind('<Return>', self.search) | |
root.bind('<Escape>', self.quit) | |
# Force window to the foreground | |
keyboard.send('alt', do_release=False) | |
ctypes.windll.user32.SetForegroundWindow(root.winfo_id()) | |
keyboard.send('alt', do_press=False) | |
entry.focus_set() | |
root.mainloop() | |
def search(self, ev): | |
text = self.entry.get() | |
webbrowser.open('https://google.com/search?' + urllib.parse.urlencode({'q': text})) | |
self.quit(None) | |
def quit(self, ev): | |
self.root.destroy() | |
self.hotkey_state.window_visible = False | |
## This should work but meanwhile a workaround is used | |
# keyboard.add_hotkey('ctrl, ctrl', Wnd, suppress=True, trigger_on_release=True) | |
class HotkeyState: | |
def __init__(self): | |
self.window_visible = False | |
self.time = 0 | |
def keyup(self, ev): | |
if 'ctrl' in ev.name: | |
if not self.window_visible: | |
if ev.time - self.time < 0.5: | |
self.window_visible = True | |
Thread(target=Wnd, args=(self,)).start() | |
else: | |
self.time = ev.time | |
else: | |
self.time = 0 | |
listener = HotkeyState() | |
keyboard.on_release(listener.keyup) | |
keyboard.wait() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment