-
Notifications
You must be signed in to change notification settings - Fork 6
/
virtual-keyboard.lua
916 lines (713 loc) · 34.9 KB
/
virtual-keyboard.lua
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
module(..., package.seeall)
--[[
Simple helper class that creates a virtual keyboard on the screen. This is useful for getting user input without the need of using the native
input methods of the devices.
The onScreenKeyboard class provides keyboards for upper letters, for small letters, for numbers and for a wide range of special characters such as the
question mark, different kinds of brackets or the @ sign.
You need a keyboard with red keys an yellow text on it? No problem. The onScreenKeyboard is fully customizable and gives you the freedom of selecting
its color, position, alpha value and behaviour. This makes it easy to create an input keyboard in the visual style of your App.
Sample usage:
<< main.lua
require("onScreenKeyboard") -- include the onScreenKeyboard.lua file
--create a textfield for the content created with the keyoard
local textField = display.newText("", 0, 0, display.contentWidth, 100, native.systemFont, 50)
textField:setTextColor(255,255,255)
--create an instance of the onScreenKeyboard class without any parameters. This creates a keyboard
--with the default values. You can manipulate the visual representation of the keyboard by passing a table to the new() function.
--Read more about this in the section "customizing the keyboard style"
local keyboard = onScreenKeyboard:new()
--create a listener function that receives the events of the keyboard
local listener = function(event)
if(event.phase == "ended") then
textField.text=keyboard:getText() --update the textfield with the current text of the keyboard
textField:setReferencePoint(display.TopLeftReferencePoint)
textField.x = 0
textField.y = 0
--check whether the user finished writing with the keyboard. The inputCompleted
--flag of the keyboard is set to true when the user touched its "OK" button
if(event.target.inputCompleted == true) then
print("Input of data complete...")
keyboard:destroy()
end
end
end
--let the onScreenKeyboard know about the listener
keyboard:setListener( listener )
--show a keyboard with small printed letters as default. Read more about the possible values for keyboard types in the section "possible keyboard types"
keyboard:drawKeyBoard(keyboard.keyBoardMode.letters_small)
>>>>>>>>>>>>>>>
customizing the keyboard style
If you create an instance of the onScreenKeyboard class without any parameters by calling onScreenKeyboard:new( )
the keyboard is created with default values (as in the example above).
To customize the style and behaviour you simply can apply a table with one or more of the following keys.
1. MAX_TEXT_SIZE
A numerical value to specify the maximum allowed length of the input text. If not specified, the size of the allowed thext is not limited
Default value is: 0 which means the text length is unlimited
2. BGCOLOR
An array with 3 numerical values in the range from 0 to 255 that adjust the RGB-colors for the background of the keyboard buttons
Default value is: { 125, 125, 125 }
3. FGCOLOR
An array with 3 numerical values in the range from 0 to 255 that adjust the RGB-colors for the text at the keyboard buttons
Default value is: { 255, 255, 255 }
4. ALPHA
An numerical value in the range of 0 to 1 that specifies the alpha value of the keyboard buttons
Default value is: 0.6
5. START_X
An numerical value that specifies the x position of the top left corner of the keyboard
Default value is: 0
6. START_Y
An numerical value that specifies the y position of the top left corner of the keyboard
Default value is: display.contentHeight/3
7. KEY_SPACE
An numerical value that specifies the space between the keys of the keyboard in pixels
Default value is: 5
8. PARENT
A reference to a display group where the created objects of the keyboard will be integrated too.
Default value is: nil
9. IGNORE_ORIENTATION_CHANGES
A boolean value that specifies whether the keyboard redraws itself automatically when it detects an orientation change of the device.
Default value is: false which means the keyboard is automatically redrawn when the device turns its orientation.
ATTENTION: To make this work you need to specify the supported orientations in your build.settings file.
Read more about his here http://developer.coronalabs.com/content/configuring-projects#Orientation_Support_iOS
Example of creating a customized keyboard
onScreenKeyboard:new( { BGCOLOR={255,0,0}, MAX_TEXT_SIZE = 5}) creates a keyboard with red keys and a maximum text length of 5 signs
possible keyboard types
After the creation of an instance of the onScreenKeyboard class you must specify the type of the keyboard that is shown first. You need to do this by calling the "drawKeyBoard" method.
This method takes one of the 4 following values.
1. keyBoardMode.letters_small - Draws a keyboard with small printed letters
2. keyBoardMode.letters_large - Draws a keyboard with large printed letters
3. keyBoardMode.letters_numbers - Draws a keyboard with a numerical input pad
4. keyBoardMode.letters_signs - Draws a keyboard with a table of special characters (such as ? ! % ...)
Methods you need to know
The onScreenKeyboard class provides some methods you may want to use in your code
1. setListener( listener )
Sets up a listener method that will be notified about keyboard events (user touches)
2. getText()
Returns the current text the user wrote with the keyboard
3. drawKeyBoard( type )
Draws a keyboard of the specified type. For more information about this task please read the section "possible keyboard types"
4.destroy()
Destorys the keyboard and clears all references to it
5. hide()
Hides the keyboard. This method sets the keyboard invisible, but it is still there
6. show()
Makes a previously hidden keyboard visible again
7. All other methods are for internal use only. Don't care about them
]]--
--[[
author - andreas ermrich
author mail - aeh@incowia.com
company - incowia GmbH / Garamox
websites - http://www.incowia.com, http://www.garamox.de
version - 1.0
date - 2012.08.08
author comments - use it for free and enjoy. Please give me your feedback with a short mail!!!
]]--
function onScreenKeyboard:new(params)
local object = {
text = "" ,
listener = nil ,
displayGroup = nil ,
parent = nil ,
btnBgColor = {125,125,125} ,
btnBgAlpha = 0.6 ,
btnFgColor = {255,255,255} ,
btnFontName = "Arial" ,
keyBoardMode = {letters_small = 1, letters_large = 2, numbers = 3, signs = 4} ,
maxTextLength = 0 ,
startX = 0 ,
startY = (display.contentHeight/3) ,
breakerItem = "BREAK" ,
keySpace = 5 ,
animationDuration = 100 ,
textUpdater = nil ,
userListenerCaller = nil ,
currentKeyboardMode = nil
}
--[[
Constructor of this class
]]--
function object:init(params)
-- function to check numerical values to be in the range from 0 to 255
local rangCheck = function(val)
local checkValue=false
if(type(val) == "number" and val >= 0 and val <=255) then
checkValue = true
end
return checkValue
end
--set up the listener that updates the self.text variable
self.textUpdater = function(event)
if(event.phase == "began") then
if(self.maxTextLength > 0 and string.len(self.text) == self.maxTextLength) then return end
self.text = self.text .. event.target.character
end
return false
end
--set up the listener that calls the user listener function for key touch events
self.userListenerCaller = function(event)
if(self.listener ~= nil) then
event.key = event.target.character
return self.listener(event)
end
return false
end
--check the given params
if(params.BGCOLOR ~= nil) then
if(type(params.BGCOLOR) == "table" and table.getn(params.BGCOLOR) == 3 and rangCheck(params.BGCOLOR[1]) == true and rangCheck(params.BGCOLOR[2]) == true and rangCheck(params.BGCOLOR[3]) == true) then
self.btnBgColor = params.BGCOLOR
else
print("param BGCOLOR is wrong")
end
end
if(params.FGCOLOR ~= nil) then
if(type(params.FGCOLOR) == "table" and table.getn(params.FGCOLOR) == 3 and rangCheck(params.FGCOLOR[1]) == true and rangCheck(params.FGCOLOR[2]) == true and rangCheck(params.FGCOLOR[3]) == true) then
self.btnFgColor = params.FGCOLOR
else
print("param FGCOLOR is wrong")
end
end
if(params.ALPHA ~= nil) then
if(type(params.ALPHA) == "number" and params.ALPHA >=0 and params.ALPHA <= 1) then
self.btnBgAlpha = params.ALPHA
else
print("param ALPHA is wrong")
end
end
if(params.START_X ~= nil) then
if(type(params.START_X) == "number") then
self.startX = params.START_X
else
print("param START_X is wrong")
end
end
if(params.START_Y ~= nil) then
if(type(params.START_Y) == "number") then
self.startY = params.START_Y
else
print("param START_Y is wrong")
end
end
if(params.KEY_SPACE ~= nil) then
if(type(params.KEY_SPACE) == "number" and params.KEY_SPACE >= 0) then
self.keySpace = params.KEY_SPACE
else
print("param KEY_SPACE is wrong")
end
end
if(params.MAX_TEXT_SIZE ~= nil) then
if(type(params.MAX_TEXT_SIZE) == "number" and params.MAX_TEXT_SIZE > 0) then
self.maxTextLength = params.MAX_TEXT_SIZE
else
print("param MAX_TEXT_SIZE is wrong")
end
end
if(params.PARENT ~= nil) then
if(type(params.PARENT) == "table") then
self.parent = params.PARENT
else
print("param PARENT is wrong")
end
end
local activateOrientationListener = true
if(params.IGNORE_ORIENTATION_CHANGES ~= nil) then
if(type(params.IGNORE_ORIENTATION_CHANGES) == "boolean")then
activateOrientationListener = not params.IGNORE_ORIENTATION_CHANGES
else
print("param IGNORE_ORIENTATION_CHANGES is wrong")
end
end
if(activateOrientationListener == true) then
local cl = function(event)
return self:orientationChange(event)
end
Runtime:addEventListener("orientation", cl)
end
self.displayGroup = display.newGroup()
end
--[[
Setter for the user listener that is notified when the user touches a key
]]--
function object:setListener(newListener)
if(newListener ~= nil and type(newListener) == "function") then
self.listener = newListener
end
end
--[[
Getter for self.text
]]--
function object:getText()
return self.text
end
--[[
Event listener for the global system event "orientation change"
]]--
function object:orientationChange(event)
self.startY = display.contentHeight/3 -- we need to reset the vertical start position for the virtual keyboard
local mode= self.keyBoardMode.letters_small
if(self.currentKeyboardMode ~= nil) then
mode = self.currentKeyboardMode
end
self:drawKeyBoard(mode)
return false
end
--[[
Factory method to create a display group for a keyboard button
]]--
function object:createButton(sign, width, height)
local buttonGroup = display.newGroup()
local backGround = display.newRect(0,0,width,height)
backGround:setReferencePoint(display.TopLeftReferencePoint)
backGround:setFillColor(self.btnBgColor[1], self.btnBgColor[2], self.btnBgColor[3])
buttonGroup:insert(backGround)
local btnText = display.newText(sign, 0, 0, native.systemFont, height/3)
btnText:setTextColor(self.btnFgColor[1], self.btnFgColor[2], self.btnFgColor[3])
btnText:setReferencePoint(display.CenterReferencePoint)
btnText.x = backGround.x + backGround.width/2
btnText.y = backGround.y + backGround.height/2
buttonGroup:insert(btnText)
--Make sure that the text is not larger than the surrounding background
while(btnText.width > backGround.width or btnText.width > backGround.height) do
btnText.size= btnText.size-1
end
--animation that will be shown, when the user touches a key button
local touchAnimation = function(event)
local btnGroup = event.target
if(event.phase == "began")then
if(self.btnBgAlpha <= 0.5) then
btnGroup.alpha = 1
else
btnGroup.alpha = 0
end
transition.to(btnGroup, {alpha=self.btnBgAlpha, time=250})
end
return false
end
buttonGroup.alpha = self.btnBgAlpha
buttonGroup:addEventListener("touch", touchAnimation)
buttonGroup:setReferencePoint(display.topLeftReferencePoint)
return buttonGroup
end
--[[
Clears all currently created keys of the keyboard
]]--
function object:clearCurrentKeyBoard()
for i=self.displayGroup.numChildren,1,-1 do
self.displayGroup:remove(i)
end
self.displayGroup = display.newGroup()
end
--[[
Wrapper that calls the concrete drawing methods that generate a virtual keyboard
]]--
function object:drawKeyBoard(mode)
local width = display.contentWidth
local height = display.contentHeight
local workingClosure= function()
self:clearCurrentKeyBoard() --remove the currently displayed keyboard buttons
if(mode == self.keyBoardMode.letters_small) then
self.currentKeyboardMode = mode
self:drawLetterKeyboard(width, height, true)
end
if(mode == self.keyBoardMode.letters_large) then
self.currentKeyboardMode = mode
self:drawLetterKeyboard(width, height, false)
end
if(mode == self.keyBoardMode.numbers) then
self.currentKeyboardMode = mode
self:drawNumberKeyboard(width, height)
end
if(mode == self.keyBoardMode.signs) then
self.currentKeyboardMode = mode
self:drawSignsKeyboard(width, height)
end
transition.from(self.displayGroup, {alpha=0, time=self.animationDuration})
if(self.parent ~= nil) then
self.parent:insert(self.displayGroup)
end
end
if(self.displayGroup ~= nil) then
transition.to(self.displayGroup, {alpha=0, time=self.animationDuration, onComplete=workingClosure})
else
workingClosure()
end
end
--[[
Create the keys for a virtual keyboard that contains small printed letters
]]--
function object:drawLetterKeyboard(width, height, smallLetters)
local numberOfKeyRowsToDraw = 4
local startX = self.startX
local startY = self.startY
local startXOrg = self.startX
local lettersToDraw = {"q", "w", "e", "r", "t", "y", "u", "i", "o", "p", self.breakerItem, "a", "s", "d", "f", "g", "h", "j", "k", "l", self.breakerItem, "z", "x", "c", "v", "b", "n", "m"}
--check how many objects the longest button row will consist of
local firstRowLength=0
for i=1,#lettersToDraw do
if(lettersToDraw[i] == self.breakerItem) then
firstRowLength = i-1
break
end
end
--create necessary counter vars for the following creation process loop
local totalRows = 0
local buttonsOfRow = 0
--check how width and height the buttons can be in maximum
local maxButtonWidth = ( (display.contentWidth -self.startX) - ((firstRowLength-1) * self.keySpace) ) / firstRowLength
local maxButtonHeight = (display.contentHeight - self.startY - (numberOfKeyRowsToDraw*self.keySpace)) / ( numberOfKeyRowsToDraw )
--create the keys for each necessary element
for i=1,#lettersToDraw do
local currentItem = lettersToDraw[i]
--check whether to use small or large letters
if(smallLetters == false)then currentItem = string.upper(currentItem) end
if(currentItem ~= self.breakerItem) then
local btnGroup = self:createButton(currentItem, maxButtonWidth, maxButtonHeight)
btnGroup.x = startX + (maxButtonWidth*buttonsOfRow) + (buttonsOfRow * self.keySpace)
btnGroup.y = startY + (maxButtonHeight*totalRows) + (totalRows * self.keySpace)
btnGroup.character = currentItem
btnGroup.inputCompleted=false
btnGroup:addEventListener("touch", self.textUpdater)
self.displayGroup:insert(btnGroup)
--call the user defined listener for the key touch events
btnGroup:addEventListener("touch", self.userListenerCaller)
buttonsOfRow = buttonsOfRow+1
else
buttonsOfRow = 0
totalRows = totalRows +1
if(totalRows == 1) then
startX = (maxButtonWidth/2 + self.keySpace) + startXOrg
else
startX = ((maxButtonWidth * 1.5) + (2 * self.keySpace)) + startXOrg
end
end
end
--draw SHIFT-Button
local btnGroup = self:createButton("SHIFT", maxButtonWidth*1.5+self.keySpace, maxButtonHeight)
btnGroup.x = startXOrg
btnGroup.y = self.startY + (maxButtonHeight*totalRows) + (totalRows * self.keySpace)
btnGroup.inputCompleted=false
if(smallLetters == true)then
btnGroup.nextKeyboardStyle = self.keyBoardMode.letters_large
else
btnGroup.nextKeyboardStyle = self.keyBoardMode.letters_small
end
--listener function for the SHIFT button
local changeKeyBoard = function(event)
if(event.phase == "ended") then
self:drawKeyBoard(event.target.nextKeyboardStyle)
end
return false
end
btnGroup:addEventListener("touch", changeKeyBoard)
self.displayGroup:insert(btnGroup)
--draw the delete button
local btnGroup = self:createButton("DEL", maxButtonWidth*1.5, maxButtonHeight)
btnGroup.x = (8.5 * maxButtonWidth + ((firstRowLength-1) * self.keySpace)) + startXOrg
btnGroup.y = self.startY + (maxButtonHeight*totalRows) + (totalRows * self.keySpace)
btnGroup.character = ""
btnGroup.inputCompleted=false
--listener function for the delete button
local deleteSign= function(event)
if(event.phase == "ended") then
self.text = string.sub(self.text, 1, string.len(self.text)-1)
end
return false
end
btnGroup:addEventListener("touch", deleteSign)
self.displayGroup:insert(btnGroup)
totalRows = totalRows +1
buttonsOfRow = 0
--call the user defined listener for the key touch events
btnGroup:addEventListener("touch", self.userListenerCaller)
--draw the keyboard switch button
local btnGroup = self:createButton("123...", maxButtonWidth*1.5+self.keySpace, maxButtonHeight)
btnGroup.x = startXOrg
btnGroup.y = self.startY + (maxButtonHeight*totalRows) + (totalRows * self.keySpace)
btnGroup.inputCompleted=false
--listener function for the keyboard switch button
local changeKeyBoard = function(event)
if(event.phase == "ended") then
self:drawKeyBoard(self.keyBoardMode.numbers)
end
end
btnGroup:addEventListener("touch", changeKeyBoard)
self.displayGroup:insert(btnGroup)
--draw the space button
local btnGroup = self:createButton("[ ]", maxButtonWidth*5 + 4*self.keySpace, maxButtonHeight)
btnGroup.x = (startXOrg + maxButtonWidth*1.5) + 2*self.keySpace
btnGroup.y = self.startY + (maxButtonHeight*totalRows) + (totalRows * self.keySpace)
btnGroup:addEventListener("touch", self.textUpdater)
btnGroup.character = " "
btnGroup.inputCompleted=false
self.displayGroup:insert(btnGroup)
--call the user defined listener for the key touch events
btnGroup:addEventListener("touch", self.userListenerCaller)
--draw the enter button that puts in a line break into the text the keyboard
local btnGroup = self:createButton("ENTER", maxButtonWidth*2+self.keySpace, maxButtonHeight)
btnGroup.x = (6.5 * maxButtonWidth + (7 * self.keySpace)) + startXOrg
btnGroup.y = self.startY + (maxButtonHeight*totalRows) + (totalRows * self.keySpace)
btnGroup:addEventListener("touch", self.textUpdater)
btnGroup.character = "\n"
btnGroup.inputCompleted=false
self.displayGroup:insert(btnGroup)
--draw the done button that closes the keyboard
local btnGroup = self:createButton("OK", maxButtonWidth*1.5, maxButtonHeight)
btnGroup.x = (8.5 * maxButtonWidth + ((firstRowLength-1) * self.keySpace)) + startXOrg
btnGroup.y = self.startY + (maxButtonHeight*totalRows) + (totalRows * self.keySpace)
btnGroup.character = ""
btnGroup.inputCompleted=true
self.displayGroup:insert(btnGroup)
--call the user defined listener for the key touch events
btnGroup:addEventListener("touch", self.userListenerCaller)
end
--[[
Create the keys for a virtual keyboard that contains small printed letters
]]--
function object:drawNumberKeyboard(width, height)
local numberOfKeyRowsToDraw = 5
local startX = self.startX
local startY = self.startY
local startXOrg = self.startX
local lettersToDraw = {"7", "8", "9", self.breakerItem, "4", "5", "6", self.breakerItem, "1", "2", "3", self.breakerItem, "0"}
--check how many objects the longest button row will consist of
local firstRowLength=0
for i=1,#lettersToDraw do
if(lettersToDraw[i] == self.breakerItem) then
firstRowLength = i-1
break
end
end
--create necessary counter vars for the following creation process loop
local totalRows = 0
local buttonsOfRow = 0
--check how width and height the buttons can be in maximum
local maxButtonWidth = ( (display.contentWidth -self.startX) - ((firstRowLength-1) * self.keySpace) ) / firstRowLength
local maxButtonHeight = (display.contentHeight - self.startY - (numberOfKeyRowsToDraw*self.keySpace)) / ( numberOfKeyRowsToDraw )
--create the keys for each necessary element
for i=1,#lettersToDraw do
local currentItem = lettersToDraw[i]
--check whether to use small or large letters
if(smallLetters == false)then currentItem = string.upper(currentItem) end
if(currentItem ~= self.breakerItem) then
local btnGroup = self:createButton(currentItem, maxButtonWidth, maxButtonHeight)
btnGroup.x = startX + (maxButtonWidth*buttonsOfRow) + (buttonsOfRow * self.keySpace)
btnGroup.y = startY + (maxButtonHeight*totalRows) + (totalRows * self.keySpace)
btnGroup.character = currentItem
btnGroup.inputCompleted=false
btnGroup:addEventListener("touch", self.textUpdater)
self.displayGroup:insert(btnGroup)
--call the user defined listener for the key touch events
btnGroup:addEventListener("touch", self.userListenerCaller)
buttonsOfRow = buttonsOfRow+1
else
buttonsOfRow = 0
totalRows = totalRows +1
if(totalRows == 3) then
startX = (maxButtonWidth + self.keySpace) + startXOrg
end
end
end
totalRows = totalRows +1
buttonsOfRow = 0
--draw the keyboard switch button
local btnGroup = self:createButton("?!(...", maxButtonWidth/2, maxButtonHeight)
btnGroup.x = startXOrg
btnGroup.y = self.startY + (maxButtonHeight*totalRows) + (totalRows * self.keySpace)
btnGroup.inputCompleted=false
--listener function for the keyboard switch button
local changeKeyBoard = function(event)
if(event.phase == "ended") then
self:drawKeyBoard(self.keyBoardMode.signs)
end
end
btnGroup:addEventListener("touch", changeKeyBoard)
self.displayGroup:insert(btnGroup)
--draw the space button
local btnGroup = self:createButton("[ ]", maxButtonWidth/2 - self.keySpace, maxButtonHeight)
btnGroup.x = (startXOrg + maxButtonWidth/2) + self.keySpace
btnGroup.y = self.startY + (maxButtonHeight*totalRows) + (totalRows * self.keySpace)
btnGroup:addEventListener("touch", self.textUpdater)
btnGroup.character = " "
btnGroup.inputCompleted=false
self.displayGroup:insert(btnGroup)
--call the user defined listener for the key touch events
btnGroup:addEventListener("touch", self.userListenerCaller)
--draw the delete button
local btnGroup = self:createButton("DEL", maxButtonWidth, maxButtonHeight)
btnGroup.x = maxButtonWidth + self.keySpace + startXOrg
btnGroup.y = self.startY + (maxButtonHeight*totalRows) + (totalRows * self.keySpace)
btnGroup.character = ""
btnGroup.inputCompleted=false
--listener function for the delete button
local deleteSign= function(event)
if(event.phase == "ended") then
self.text = string.sub(self.text, 1, string.len(self.text)-1)
end
return false
end
btnGroup:addEventListener("touch", deleteSign)
self.displayGroup:insert(btnGroup)
--call the user defined listener for the key touch events
btnGroup:addEventListener("touch", self.userListenerCaller)
--draw the enter button that puts in a line break into the text the keyboard
local btnGroup = self:createButton("ENTER", maxButtonWidth/2, maxButtonHeight)
btnGroup.x = (2 * maxButtonWidth + (2*self.keySpace)) + startXOrg
btnGroup.y = self.startY + (maxButtonHeight*totalRows) + (totalRows * self.keySpace)
btnGroup:addEventListener("touch", self.textUpdater)
btnGroup.character = "\n"
btnGroup.inputCompleted=false
self.displayGroup:insert(btnGroup)
--draw the done button that closes the keyboard
local btnGroup = self:createButton("OK", maxButtonWidth/2, maxButtonHeight)
btnGroup.x = 2.5 * maxButtonWidth + 3*self.keySpace + startXOrg
btnGroup.y = self.startY + (maxButtonHeight*totalRows) + (totalRows * self.keySpace)
btnGroup.character = ""
btnGroup.inputCompleted=true
self.displayGroup:insert(btnGroup)
--call the user defined listener for the key touch events
btnGroup:addEventListener("touch", self.userListenerCaller)
end
--[[
Create the keys for a virtual keyboard that contains small printed letters
]]--
function object:drawSignsKeyboard(width, height)
local numberOfKeyRowsToDraw = 5
local startX = self.startX
local startY = self.startY
local startXOrg = self.startX
local lettersToDraw = {"!" , "?" , "&" , "$" , "@" , "%" , "[" , "]" , self.breakerItem ,
"(" , ")" , "{" , "}" , "/" , "\\" , "\"" , "," , self.breakerItem ,
";" , ":" , "." , "~" , "#" , "'" , "`" , "*" , self.breakerItem ,
"+" , "-" , "=" , "<" , ">" , "|" , "_"
}
--check how many objects the longest button row will consist of
local firstRowLength=0
for i=1,#lettersToDraw do
if(lettersToDraw[i] == self.breakerItem) then
firstRowLength = i-1
break
end
end
--create necessary counter vars for the following creation process loop
local totalRows = 0
local buttonsOfRow = 0
--check how width and height the buttons can be in maximum
local maxButtonWidth = ( (display.contentWidth -self.startX) - ((firstRowLength-1) * self.keySpace) ) / firstRowLength
local maxButtonHeight = (display.contentHeight - self.startY - (numberOfKeyRowsToDraw*self.keySpace)) / ( numberOfKeyRowsToDraw )
--create the keys for each necessary element
for i=1,#lettersToDraw do
local currentItem = lettersToDraw[i]
--check whether to use small or large letters
if(smallLetters == false)then currentItem = string.upper(currentItem) end
if(currentItem ~= self.breakerItem) then
local btnGroup = self:createButton(currentItem, maxButtonWidth, maxButtonHeight)
btnGroup.x = startX + (maxButtonWidth*buttonsOfRow) + (buttonsOfRow * self.keySpace)
btnGroup.y = startY + (maxButtonHeight*totalRows) + (totalRows * self.keySpace)
btnGroup.character = currentItem
btnGroup.inputCompleted=false
btnGroup:addEventListener("touch", self.textUpdater)
self.displayGroup:insert(btnGroup)
--call the user defined listener for the key touch events
btnGroup:addEventListener("touch", self.userListenerCaller)
buttonsOfRow = buttonsOfRow+1
else
buttonsOfRow = 0
totalRows = totalRows +1
end
end
totalRows = totalRows +1
buttonsOfRow = 0
--draw the keyboard switch button
local btnGroup = self:createButton("abc...", 2.5*maxButtonWidth, maxButtonHeight)
btnGroup.x = startXOrg
btnGroup.y = self.startY + (maxButtonHeight*totalRows) + (totalRows * self.keySpace)
btnGroup.inputCompleted=false
--listener function for the keyboard switch button
local changeKeyBoard = function(event)
if(event.phase == "ended") then
self:drawKeyBoard(self.keyBoardMode.letters_small)
end
end
btnGroup:addEventListener("touch", changeKeyBoard)
self.displayGroup:insert(btnGroup)
--draw the space button
local btnGroup = self:createButton("[ ]", maxButtonWidth, maxButtonHeight)
btnGroup.x = (startXOrg + 2.5*maxButtonWidth) + self.keySpace
btnGroup.y = self.startY + (maxButtonHeight*totalRows) + (totalRows * self.keySpace)
btnGroup:addEventListener("touch", self.textUpdater)
btnGroup.character = " "
btnGroup.inputCompleted=false
self.displayGroup:insert(btnGroup)
--call the user defined listener for the key touch events
btnGroup:addEventListener("touch", self.userListenerCaller)
--draw the delete button
local btnGroup = self:createButton("DEL", maxButtonWidth, maxButtonHeight)
btnGroup.x = (startXOrg + 3.5*maxButtonWidth) + 2*self.keySpace
btnGroup.y = self.startY + (maxButtonHeight*totalRows) + (totalRows * self.keySpace)
btnGroup.character = ""
btnGroup.inputCompleted=false
--listener function for the delete button
local deleteSign= function(event)
if(event.phase == "ended") then
self.text = string.sub(self.text, 1, string.len(self.text)-1)
end
return false
end
btnGroup:addEventListener("touch", deleteSign)
self.displayGroup:insert(btnGroup)
--call the user defined listener for the key touch events
btnGroup:addEventListener("touch", self.userListenerCaller)
--draw the enter button that puts in a line break into the text the keyboard
local btnGroup = self:createButton("ENTER", maxButtonWidth, maxButtonHeight)
btnGroup.x = (startXOrg + 4.5*maxButtonWidth) + 3*self.keySpace
btnGroup.y = self.startY + (maxButtonHeight*totalRows) + (totalRows * self.keySpace)
btnGroup:addEventListener("touch", self.textUpdater)
btnGroup.character = "\n"
btnGroup.inputCompleted=false
self.displayGroup:insert(btnGroup)
--draw the done button that closes the keyboard
local btnGroup = self:createButton("OK", 2.5*maxButtonWidth+2*self.keySpace, maxButtonHeight)
btnGroup.x = (startXOrg + 5.5*maxButtonWidth) + 4*self.keySpace
btnGroup.y = self.startY + (maxButtonHeight*totalRows) + (totalRows * self.keySpace)
btnGroup.character = ""
btnGroup.inputCompleted=true
self.displayGroup:insert(btnGroup)
--call the user defined listener for the key touch events
btnGroup:addEventListener("touch", self.userListenerCaller)
end
--[[
Destroy the entire keyboard
]]--
function object:destroy()
local cl = function()
self:clearCurrentKeyBoard()
self = nil
end
if(self.displayGroup ~= nil) then
transition.to(self.displayGroup, {alpha = 0, time=self.animationDuration, onComplete=cl})
end
end
--[[
Hide the entire keyboard
]]--
function object:hide()
if(self.displayGroup ~= nil) then
transition.to(self.displayGroup, {alpha = 0, time=self.animationDuration})
end
end
--[[
Show the entire keyboard
]]--
function object:show()
if(self.displayGroup ~= nil) then
transition.to(self.displayGroup, {alpha=1, time=self.animationDuration})
end
end
--check parameters
if(params~=nil) then
object:init(params)
else
object:init({})
end
return object
end