how to create virtual keyboard using python, tkinter and pyautogui - part - 2

hello friends,

                         

                                          This Is Our Second part Of Virtual keyboard using python tutorial series. or you can say how to create virtual keyboard using python, tkinter and pyautogui module


and this is our second part.

if you are new visitor. then, first check our first part .

or

directly check our index.

but first, let me clear. what we have learnt in 1 part.

In 1st part,


 we worked on GUI (Graphic User Interface).

as screenshot given below.






And In 2nd Part, (Current part)


We will Work on virtual keyboard events.


and for this i am using pyautogui python module.

so, first you need to install pyautogui.


Mac OS X

    To Install this module on Mac OS X, you need the PyObjC module installed.
 
For Python 3, run:

sudo pip3 install pyobjc-core

sudo pip3 install pyobjc

sudo pip3 install pyautogui


    For Python 2, run:

sudo pip install pyobjc-core

sudo pip install pyobjc

sudo pip install pyautogui

 (There's some bug with their installer, so install pyobjc-core first or else
    the install takes forever.)
 

Linux

    To install this module on Linux, you need Xlib module installed.

    For Python 3, run:
   
sudo apt-get install Xlib

sudo pip3 install python3-Xlib

sudo pip3 install pyautogui
 
    For Python 2, run:


sudo apt-get install Xlib

sudo pip install Xlib

sudo pip3 install pyautogui

Windows.


    To install this module on Windows, you do not need anything else.
    

sudo pip3 install pyautogui



So, let's move to script codes.

here basically,

i am adding some codes and features on virtual keyboard example.

if you want to see previous example then check our first part .

let me explain you what features i have added in current codes.

1. transparency

2. focus

3. key button working (using pyautogui python module.)

4. looks

here, i am sharing my Screen Shot.



and

here its my

Code 1.



  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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/python
# ---------------- READ ME ---------------------------------------------
# This Script is Created Only For Practise And Educational Purpose Only
# This Script Is Created For http://bitforestinfo.blogspot.com
# This Script is Written By
#
#
##################################################
######## Please Don't Remove Author Name #########
############### Thanks ###########################
##################################################
#
#
__author__='''

######################################################
                By S.S.B Group                          
######################################################

    Suraj Singh
    Admin
    S.S.B Group
    surajsinghbisht054@gmail.com
    http://bitforestinfo.blogspot.com/

    Note: We Feel Proud To Be Indian
######################################################
'''


# ========== Configurations ====================
BUTTON_BACKGROUND   = "black"
MAIN_FRAME_BACKGROUND  = "cornflowerblue"
BUTTON_LOOK    = "flat" #flat, groove, raised, ridge, solid, or sunken
TOP_BAR_TITLE    = "Python Virtual KeyBoard."
TOPBAR_BACKGROUND   = "skyblue"
TRANSPARENCY    = 0.7
FONT_COLOR     = "white"

# ==============================================

# import modules
try:
    import Tkinter
except:
    import tkinter as Tkinter

import pyautogui

keys =[ 
[
# =========================================
# ===== Keyboard Configurations ===========
# =========================================

 [
  # Layout Name
  ("Function_Keys"),

  # Layout Frame Pack arguments
  ({'side':'top','expand':'yes','fill':'both'}),
  [
   # list of Keys
   ('esc', 'F1', 'F2','F3','F4','F5','F6','F7','F8','F9','F10','F11','F12')
  ]
 ],

 [
  ("Character_Keys"),
  ({'side':'top','expand':'yes','fill':'both'}),
  [
   ('`',',','.','/','-','=','\\','[',']','backspace'),
   ('~','!','@','#','$','%','^','&','*','(',')','_','+','|'),
   ('tab','q','w','e','r','t','y','u','i','o','p','{','}',";",'\''),
   ('capslock','a','s','d','f','g','h','j','k','l',':',"\"","enter"),
   ("shift",'z','x','c','v','b','n','m','<','>','?',"shift"),
   ("ctrl", "win",'alt','space  ','alt','win','[=]','ctrl')
  ]
 ]
],
[
 [
  ("System_Keys"),
  ({'side':'top','expand':'yes','fill':'both'}),
  [
   (
    "printscreen",
    "scrolllock",
    "pause"
   )
  ]
 ],
 [          
  ("Editing_Keys"),
  ({'side':'top','expand':'yes','fill':'both'}),
  [
   (
    "insert",
    "home",
    "pageup"
    ),
   ( "delete",
    "end",
    "pagedown"
    ),

  ]
 ],

 [
  ("Navigation_Keys"),
  ({'side':'top','expand':'yes','fill':'both'}),
  [
   (
    "up",
    ),
   ( "right",
    "down",
    "left"
    ),
  ]
 ],

],
[

 [
  ("Numeric_Keys"),
  ({'side':'top','expand':'yes','fill':'both'}),
  [
   ("numlock","/","*"),
   ("7","8","9","+"),
   ("4","5","6","-"),
   ("1","2","3","0"),
   (".","enter")
  ]
 ],

]

]

# Create key event
def create_keyboard_event(numlock, capslock, controler, key):
 return

##  Frame Class
class Keyboard(Tkinter.Frame):
 def __init__(self, *args, **kwargs):
  Tkinter.Frame.__init__(self, *args, **kwargs)
  

  # Function For Creating Buttons
  self.create_frames_and_buttons()



 # Function For Extracting Data From KeyBoard Table
 # and then provide us a well looking
 # keyboard gui
 def create_frames_and_buttons(self):
  # take section one by one
  for key_section in keys:
   # create Sperate Frame For Every Section
   store_section = Tkinter.Frame(self)
   store_section.pack(side='left',expand='yes',fill='both',padx=10,pady=10,ipadx=10,ipady=10)
   
   for layer_name, layer_properties, layer_keys in key_section:
    store_layer = Tkinter.LabelFrame(store_section)#, text=layer_name)
    #store_layer.pack(side='top',expand='yes',fill='both')
    store_layer.pack(layer_properties)
    for key_bunch in layer_keys:
     store_key_frame = Tkinter.Frame(store_layer)
     store_key_frame.pack(side='top',expand='yes',fill='both')
     for k in key_bunch:
      k=k.capitalize()
      if len(k)<=3:
       store_button = Tkinter.Button(store_key_frame, text=k, width=2, height=2)
      else:
       store_button = Tkinter.Button(store_key_frame, text=k.center(5,' '), height=2)
      if " " in k:
       store_button['state']='disable'
      
      store_button['relief']=BUTTON_LOOK
      store_button['bg']=BUTTON_BACKGROUND
      store_button['fg']=FONT_COLOR

      store_button['command']=lambda q=k.lower(): self.button_command(q)
      store_button.pack(side='left',fill='both',expand='yes')
  return

  # Function For Detecting Pressed Keyword.
 def button_command(self, event):
  pyautogui.press(event)
  return

class top_moving_mechanism:
 def __init__(self, root, label):
  self.root = root
  self.label = label

 def motion_activate(self, kwargs):
  w,h = (self.root.winfo_reqwidth(), self.root.winfo_reqheight())
  (x,y) = (kwargs.x_root, kwargs.y_root)
  self.root.geometry("%dx%d+%d+%d" % (w,h,x,y))
  return



# Creating Main Window
def main():
 root = Tkinter.Tk(className=TOP_BAR_TITLE)
 k=Keyboard(root, bg=MAIN_FRAME_BACKGROUND)

 # Confifuration
 root.overrideredirect(True)
 root.wait_visibility(root)
 root.wm_attributes('-alpha',TRANSPARENCY)
 # Custum
 f = Tkinter.Frame(root)
 t_bar=Tkinter.Label(f, text=TOP_BAR_TITLE, bg=TOPBAR_BACKGROUND)
 t_bar.pack(side='left',expand="yes", fill="both")
 mechanism = top_moving_mechanism(root, t_bar)
 t_bar.bind("<B1-Motion>", mechanism.motion_activate)
 Tkinter.Button(f, text="[X]", command= root.destroy).pack(side='right')
 f.pack(side='top', expand='yes',fill='both')
 k.pack(side='top')
 root.mainloop()
 return

# Function Trigger
if __name__=='__main__':
 main()


after executing this codes. 

you will got a well looking and a well working virtual codes.

as i already shared my screenshot.

if anyone from you want this updated example or want to contribute.

then, check here my repository. click here

so friends, i think this is enough for today.

In my next tutorial, i will show you more staff written in python language.

so, stay tuned with us.

and if you like my post then,  

don't forget to subscribe our blog 

and 

our youtube channel.

have a nice day.

written by:
                   S.S.B
                  surajsinghbisht054@gmail.com

For More Update, Visit Our Regularly. 

And Subscribe Our Blog, 

Follow Us and share it.
For Any Type of Suggestion Or Help
Contact me:
S.S.B
surajsinghbisht054@gmail.com



Share this

Related Posts

Previous
Next Post »