-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
165 lines (148 loc) · 5.13 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from __future__ import division, print_function, unicode_literals
import pynput.mouse as Mouse
import pynput.keyboard as Keyboard
import time
import gtk
import requests
import json
from Tkinter import *
import re
from threading import Thread
gtk.gdk.threads_init()
"""
event.value = 1: key down
event.value = 0: key up
event.value = 2: key hold
"""
# device = evdev.InputDevice('/dev/input/event6')
# for event in device.read_loop():
# if event.value == 589825:
# print(evdev.util.categorize(event))
ctrl_btn_press = False
mouse_release = False
def timed_join_all(threads, timeout):
start = cur_time = time.time()
while cur_time <= (start + timeout):
for thread in threads:
if not thread.is_alive():
thread.join()
time.sleep(1)
cur_time = time.time()
def on_move(x, y):
print('Pointer moved to {0}'.format( (x, y)))
curr = time.time()
def on_click(x, y, button, pressed):
global mouse_release
global curr
print('{0} at {1}'.format( 'Pressed' if pressed else 'Released', (x, y)))
if pressed:
mouse_release = False
print("Mouse Press")
if time.time() - curr < 0.25:
print("Double click {}".format((x,y)))
else:
curr = time.time()
else:
mouse_release = True
print("Mouse Release")
def on_scroll(x, y, dx, dy):
print('Scrolled {0}'.format((x, y)))
print('Scroll vector {0}'.format((dx, dy)))
isCatchWords = False
catchedWords = None
def on_press(key):
global ctrl_btn_press
global isCatchWords
global catchedWords
try:
# print('alphanumeric key {0} pressed'.format(key.char))
if isCatchWords:
catchedWords += key.char
if key.char == 'i' and ctrl_btn_press:
isCatchWords = True
catchedWords = ""
except AttributeError:
if key == Keyboard.Key.ctrl:
ctrl_btn_press = True
if key == Keyboard.Key.space:
catchedWords += " "
if key == Keyboard.Key.backspace:
catchedWords = catchedWords[:-1]
if key == Keyboard.Key.enter:
isCatchWords = False
print("\nCatched words: ", catchedWords)
lookup(catchedWords)
catchedWords = ""
# print('special key {0} pressed'.format(key))
def on_release(key):
global ctrl_btn_press
# print('{0} released'.format(key))
if key == Keyboard.Key.ctrl:
ctrl_btn_press = False
if key == Keyboard.Key.esc:
# Stop listener
return False
def close_when_lost_focus(event):
print("action: close widget")
parentName = event.widget.winfo_parent()
parent = event.widget._nametowidget(parentName)
parent.destroy()
return
def make_text_popup(content, lookupText):
root = Tk()
root.title("Simple Dictionary")
txt = Text(root, width=60, height=20, wrap="word")
cnt = 0
for sen in content['sentences']:
cnt += 1
en_sen = sen['fields']['en']
vi_sen = sen['fields']['vi']
if vi_sen[-1] == '\n':
display_sentence = str(cnt) + ". " + en_sen + ": " + vi_sen
else:
display_sentence = str(cnt) + ". " + en_sen + ": " + vi_sen + "\n"
txt.insert(INSERT, display_sentence)
pattern = re.compile('(\w+\-)*(' + lookupText + ')(\-\w+)*', re.IGNORECASE)
indexes = [(m.start(0), m.end(0), m.group(0)) for m in re.finditer(pattern, display_sentence)]
for (start, end, group) in indexes:
# print("{}. {} - {}".format(cnt, start, end))
txt.tag_add("look_up_text", str(cnt) + "." + str(start), str(cnt) + "." + str(end))
txt.tag_config("look_up_text", font='helvetica 11 bold', relief='raised')
txt.insert(END, "---------------------------END---------------------------")
txt.config(state=DISABLED)
txt.bind("<Leave>", close_when_lost_focus)
txt.pack()
root.mainloop()
def _clipboard_changed(clipboard, event):
global ctrl_btn_press
# global mouse_release
# print("clipboard changed")
if not ctrl_btn_press:
return
else:
ctrl_btn_press = False
text = clipboard.wait_for_text()
lookup(text)
def lookup(text):
print("Looking up for: ", text)
url = 'http://api.tracau.vn/WBBcwnwQpV89/'+text+'/en/JSON_CALLBACK'
response = requests.get(url)
if response.ok:
response.encoding = "utf-8"
content = response.text
content = content[14:-3]
content = re.compile(r'<.*?>').sub("", content)
content = json.loads(content)
if len(content['sentences']) == 0:
print("no information")
return # display popup to show result
make_text_popup(content, text)
if __name__=="__main__":
mouseListener = Mouse.Listener( on_move=on_move, on_click=on_click, on_scroll=on_scroll)
keyboardListener = Keyboard.Listener( on_press=on_press, on_release=on_release)
# mouseListener.start()
keyboardListener.start()
print("Simple Dictionary is running...")
clip = gtk.clipboard_get(gtk.gdk.SELECTION_PRIMARY)
clip.connect("owner-change", _clipboard_changed)
gtk.main()