Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/common/Util/KeyboardWindow.py
blob: 507276bdb1840eade6fc392290fecf0d321402f6 (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python
import pygtk
pygtk.require( '2.0' )
import gtk
import random
from common.Util.ThemeWidgets import keyButton
import common.Config as Config
KEY_MAP_PIANO = Config.KEY_MAP_PIANO

class KeyboardWindow(gtk.Window):
    def __init__(self, size = None, pos = 0, popup = False):
        if popup is False:
            gtk.Window.__init__(self , gtk.WINDOW_TOPLEVEL)
        else:
            gtk.Window.__init__(self , gtk.WINDOW_POPUP)
        color = gtk.gdk.color_parse("#000000")
        self.modify_bg(gtk.STATE_NORMAL, color)
        self.set_decorated(False)

        self.add_events(gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK | gtk.gdk.ENTER_NOTIFY_MASK | gtk.gdk.KEY_PRESS_MASK)
        self.connect("key-press-event",self.handle_keypress)
        self.connect("key-release-event",self.handle_keyrelease)
        self.connect("button-press-event",self.handle_mousePress)
        self.connect("button-release-event",self.handle_mouseRelease)
        self.connect("enter-notify-event",self.handle_enter)
        
        self.size = size
        self.pos = pos
        self.popup = popup
        self.set_pos(self.pos)
        
        if self.size == None:
            self.pixel_space = 15
            self.height = 45
        else:
            self.pixel_space = size
            self.height = 3 * size
        
        self.draw()
        
    def draw(self):
        self.rows = {}
        self.rows[1] = [(49,1), (10,3), (11,3), (12,3), (13,3), (14,3), (15,3), (16,3), (17,3), (18,3), (19,3), (20,3), (21,5)]
        self.rows[2] = [(23,3), (24,3), (25,3), (26,3), (27,3), (28,3), (29,3), (30,3), (31,3), (32,3), (33,3), (34,3), (35,4)]
        self.rows[3] = [(37,4), (38,3), (39,3), (40,3), (41,3), (42,3), (43,3), (44,3), (45,3), (46,3), (47,3), (48,3), (51,3)]
        self.rows[4] = [(50,6), (52,3), (53,3), (54,3), (55,3), (56,3), (57,3), (58,3), (59,3), (60,3), (61,3), (62,5)]
        self.rows[5] = [(216,1),(133,4),(64,4), (65,25), (108,4), (134,4,), (113,3)]
        
        self.right_section = [(22,7),(36,(7,7)),(111,3),(219,3),(116,3),(114,3)]
        
        self.btn_dic = {}
        
        mainvbox = gtk.VBox()
        mainhbox = gtk.HBox()
        
        #Main keyboard section
        vbox = gtk.VBox()
        for row in [1,2,3,4,5]:
            hbox = gtk.HBox()
            for key in self.rows[row]:
                self.btn_dic[key[0]] = keyButton(self.pixel_space * key[1], self.height, [0,0,0], [0.5,0.5,0.5])
                hbox.pack_start(self.btn_dic[key[0]], padding = self.pixel_space//2)
            vbox.pack_start(hbox, padding = self.pixel_space//2)
        mainhbox.pack_start(vbox)
        
        #Right part of the keyboard
        right_vbox = gtk.VBox()
        right_tophbox = gtk.HBox()
        right_lowhbox = gtk.HBox()
        
        self.btn_dic[self.right_section[0][0]] = keyButton(self.pixel_space * self.right_section[0][1], self.height, [0,0,0], [0.5,0.5,0.5])
        self.btn_dic[self.right_section[1][0]] = keyButton(self.pixel_space * self.right_section[1][1][0], self.pixel_space * self.right_section[1][1][1], [0,0,0], [0.5,0.5,0.5])
        self.btn_dic[self.right_section[2][0]] = keyButton(self.pixel_space * self.right_section[2][1], self.height, [0,0,0], [0.5,0.5,0.5])
        self.btn_dic[self.right_section[3][0]] = keyButton(self.pixel_space * self.right_section[3][1], self.height, [0,0,0], [0.5,0.5,0.5])
        self.btn_dic[self.right_section[4][0]] = keyButton(self.pixel_space * self.right_section[4][1], self.height, [0,0,0], [0.5,0.5,0.5])
        self.btn_dic[self.right_section[5][0]] = keyButton(self.pixel_space * self.right_section[5][1], self.height, [0,0,0], [0.5,0.5,0.5])
            
        right_vbox.pack_start(self.btn_dic[self.right_section[0][0]], padding = self.pixel_space//2)
        right_vbox.pack_start(self.btn_dic[self.right_section[1][0]], padding = self.pixel_space//2)
        right_tophbox.pack_start(self.btn_dic[self.right_section[2][0]], padding = self.pixel_space//2)
        right_tophbox.pack_start(self.btn_dic[self.right_section[3][0]], padding = self.pixel_space//2)
        right_lowhbox.pack_start(self.btn_dic[self.right_section[4][0]], padding = self.pixel_space//2)
        right_lowhbox.pack_start(self.btn_dic[self.right_section[5][0]], padding = self.pixel_space//2)
        right_vbox.pack_start(right_tophbox, padding = self.pixel_space//2)
        right_vbox.pack_start(right_lowhbox, padding = self.pixel_space//2)
        
        #Mouse buttons
        mouse_hbox = gtk.HBox()
        self.btn_dic["left_mouse"] = keyButton(self.pixel_space * 6, self.pixel_space * 2, [0,0,0], [0.5,0.5,0.5])
        self.btn_dic["right_mouse"] = keyButton(self.pixel_space * 6, self.pixel_space * 2, [0,0,0], [0.5,0.5,0.5])
        mouse_hbox.pack_start(self.btn_dic["left_mouse"], True, True, self.pixel_space//2)
        mouse_hbox.pack_end(self.btn_dic["right_mouse"], True, True, self.pixel_space//2)

        
        #Enter and Leave connections
        for key in self.btn_dic:
            self.btn_dic[key].connect("enter",self.handle_mouseEnter)
            self.btn_dic[key].connect("leave",self.handle_mouseLeave)
    
        mainhbox.pack_start(right_vbox)        
        mainvbox.pack_start(mainhbox)
        mainvbox.pack_start(mouse_hbox, padding = self.pixel_space//2)
        
        self.add(mainvbox)
        
    def set_pos(self,_pos = 0):
        width = self.get_screen().get_width()
        height = self.get_screen().get_height()
        win_width = self.get_size()[0]
        win_height = self.get_size()[1]
        
        self.pos = _pos
        
        pos = [0,0]
        pos[0] = (0, 0) 
        pos[1] = (width - win_width, height - win_height)
        self.move(pos[self.pos][0],pos[self.pos][1])
        
    def color_piano(self):
        for key in KEY_MAP_PIANO:
            self.btn_dic[key].set_fillcolor(1,1,1)

    
    def handle_keypress(self,widget,event):
        if event.hardware_keycode == 9: # Hide the keyboard with escape Key
            self.hide_all()
        elif event.hardware_keycode == 216: # Send a fake mouse event
            self.btn_dic["left_mouse"].set_fillcolor(random.random(),random.random(),random.random())
        elif event.hardware_keycode == 133: # Send a fake mouse event
            self.btn_dic["right_mouse"].set_fillcolor(random.random(),random.random(),random.random())
        else:
            if self.btn_dic.has_key(event.hardware_keycode):
                self.btn_dic[event.hardware_keycode].set_fillcolor(random.random(),random.random(),random.random())
    
    def handle_keyrelease(self,widget,event):
        if KEY_MAP_PIANO.has_key(event.hardware_keycode):
           self.btn_dic[event.hardware_keycode].set_fillcolor(1,1,1)
        else:
            if self.btn_dic.has_key(event.hardware_keycode):
                self.btn_dic[event.hardware_keycode].set_fillcolor(0,0,0)
            if event.hardware_keycode == 216 or event.hardware_keycode == 133:
                self.btn_dic["left_mouse"].set_fillcolor(0,0,0)
                self.btn_dic["right_mouse"].set_fillcolor(0,0,0)

    
    def handle_mousePress(self,widget,event):
        if event.button == 1:
            self.btn_dic["left_mouse"].set_fillcolor(random.random(),random.random(),random.random())
        elif event.button == 3:
            self.btn_dic["right_mouse"].set_fillcolor(random.random(),random.random(),random.random())
    
    def handle_mouseRelease(self,widget,event):
        if event.button == 1:
            self.btn_dic["left_mouse"].set_fillcolor(0,0,0)
        elif event.button == 3:
            self.btn_dic["right_mouse"].set_fillcolor(0,0,0)
        
    def handle_mouseEnter(self,widget,event = None):
        widget.set_strokecolor(1,1,1)
            
    def handle_mouseLeave(self,widget,event = None):
        widget.set_strokecolor(0.5,0.5,0.5)
        
    def handle_enter(self,widget,event):
        if self.popup is False:
            return
        if self.pos == 0:
            self.set_pos(1)
        else:
            self.set_pos(0)
        
    def close(self,widget,event = None):
        self.hide_all()
    
        
        
if __name__ == "__main__":
    win = KeyboardWindow()
    win.connect("destroy",gtk.main_quit)
    win.show_all()
    gtk.main()