keyboard

I want to share this little code I wrote with you, it moves a selector over a vitual keyboard image:

def __init__(self):
 self.keyboardMap = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', ' ', ',', '.', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', ' ', '"', '-', 'u', 'v', 'w', 'x', 'y', 'z', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ' ', ' ', ' ']

 self.lineTotal = 13
 self.total = 65

 self.offsetX = 0.06
 self.offsetZ = 0.1
  
 # the position where the letter 'a' is at your virtual keyboard image
 self.startX = -0.849
 self.startZ = 0.218

 self.keyboardImg = Onscreenimage(image = 'keyboard.png', scale = (1,1,1), pos = (0,1,0), color = (1,1,1,1), parent = render2d))
 self.keyboardImg.hide()

 self.letterSelector = Onscreenimage(image = 'letterSelector.png', scale = (0.03,1,0.05), pos = (self.startX,1,self.startZ), color = (1,1,1,1), parent = render2d))
 self.letterSelector.hide()

def open(self):
 self.letterSelector.setPos(Vec3(self.startX,1,self.startZ))
 self.letterSelector.show()

 self.keyboardImg.show()

 self.index = 0 # letter 'a'

 self.accept('w', self.__moveSelector__, ['UP'])
 self.accept('s', self.__moveSelector__, ['DOWN'])
 self.accept('a', self.__moveSelector__, ['LEFT'])
 self.accept('d', self.__moveSelector__, ['RIGHT'])
 self.accept('enter', self.__getLetter__)

def exit(self):
 self.letterSelector.hide()
 self.keyboardImg.hide()

def __getLetter__(self):
 self.selectedLetter = self.keyboardMap[self.index]

def __moveSelector__(self, direction):
 if direction == 'UP':
    self.index -= self.lineTotal # move one line up
    # jump to the last line
    if self.index < 0: self.index = self.total + self.index 
 elif direction == 'DOWN':
    self.index += self.lineTotal # move one line down
    # jump to the first line.
    # self.total - 1 because self.index starts at 0
    if self.index > (self.total - 1): self.index = self.index - self.total
 elif direction == 'LEFT':
    self.index -= 1 # move to the left
    # moves the selector to the last character of the line
    if self.index < 0: self.index += self.lineTotal
    elif ((self.index + 1) % self.lineTotal) == 0:
        self.index += self.lineTotal
 elif direction == 'RIGHT':
     self.index += 1 # move to the right
     # moves the selector to the first character of the line
     if self.index > (self.total - 1): self.index -= self.lineTotal
     elif (self.index % self.lineTotal) == 0:
         self.index -= self.lineTotal
        
 self.letterSelectorZ = self.startZ - (self.offsetZ * (self.index / self.lineTotal))
self.letterSelectorX = self.startX + (self.offsetX * (self.index % self.lineTotal))
        
 self.letterSelector.LerpPosInterval(0.2, (self.letterSelectorX,1,self.letterSelectorZ), (self.letterSelector.getX(),1,self.letterSelector.getZ()))

sorry, but i’m not really sure what this code exactly is about nor how to use it as it seems uncomplete. could you show a version that runs ‘as it is’?

sorry for lack of explanation, I will update it soon, I need time, I’m always busy… :frowning:

you can use this code to build your own 2d keyboard to enter name or other things.

here are the images, low quality:

i644.photobucket.com/albums/uu17 … lector.png
i644.photobucket.com/albums/uu17 … yboard.png