Reputation: 234
I have the following functions
def game_test_write_vocabularuy():
game_test = True
active = False
text_hiragana = ''
while game_test == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
terminate()
if event.type == pygame.KEYDOWN:
if active == True:
if event.key == pygame.K_BACKSPACE:
text_hiragana = text_hiragana[:-1]
else:
text_hiragana += event.unicode
if event.type == pygame.MOUSEBUTTONDOWN:
if input_rectangle.collidepoint(event.pos):
active = True
else:
active = False
game_display.fill(white)
if active:
pygame.draw.rect(game_display,gray,input_rectangle,2)
else:
pygame.draw.rect(game_display, red, input_rectangle, 2)
textFunc(FontJapoMedium,text_hiragana,black,input_rectangle[0] + 5,input_rectangle[1] + 5 ,False)
Clock.tick(FPS)
pygame.display.update()
Basically, it allows me to type something and it will get rendered on the sreen. Thing is, when I turn the japanese keyboard on and I start typing, it doesnt display the japanese text. Rather, it simply displays the roman letters
For example, in my keyboard D = し, however when I press the D key, instead of し, nothing is rendered; as if the event isnt registered What should I do?
Note- The font is not the problem
Upvotes: 1
Views: 959
Reputation: 234
I solved it like this:
I just used a dictionary to convert the characters to hiragana
Table = {
#A Column
'a':'あ',
'ka':'か',
'ga':'が',
'sa':'さ',
'za':'ざ',
'ta':'た',
'da':'だ',
'na':'な',
'ha':'は',
'ba':'ば',
'pa':'ぱ',
'ma':'ま',
'ya':'や',
'ra':'ら',
'wa':'わ',
#I Column
'i':'い',
'ki':'き',
'gi':'ぎ',
'shi':'し',
'ji':'じ',
'chi':'ち',
'ni':'に',
'hi':'ひ',
'bi':'び',
'pi':'ぴ',
'mi':'み',
'ri':'り',
#U Column
'u':'う',
'ku':'く',
'gu':'ぐ',
'su':'す',
'zu':'ず',
'tsu':'つ',
'nu':'ぬ',
'fu':'ふ',
'bu':'ぶ',
'pu':'ぷ',
'mu':'む',
'ru':'る',
'yu':'ゆ',
#E Column
'e':'え',
'ke':'け',
'ge':'げ',
'se':'せ',
'ze':'ぜ',
'te':'て',
'de':'で',
'ne':'ね',
'he':'へ',
'be':'べ',
'pe':'ぺ',
'me':'め',
're':'れ',
#O Column
'o':'お',
'ko':'こ',
'go':'ご',
'so':'そ',
'zo':'ぞ',
'to':'と',
'do':'ど',
'no':'の',
'ho':'ほ',
'bo':'ぼ',
'po':'ぽ',
'mo':'も',
'yo':'よ',
'ro':'ろ',
'wo':'を',
#N sound
'nn':'ん',
#Characters with 3 sounds that use the small Y characters
'kya':'きゃ',
'kyu':'きゅ',
'kyo':'きょ',
'sha':'しゃ',
'shu':'しゅ',
'sho':'しょ',
'cha':'ちゃ',
'chu':'ちゅ',
'cho':'ちょ',
'nya':'にゃ',
'nyu':'にゅ',
'nyo':'にょ',
'hya':'ひゃ',
'hyu':'ひゅ',
'hyo':'ひょ',
'mya':'みゃ',
'myu':'みゅ',
'myo':'みょ',
'rya':'りゃ',
'ryu':'りゅ',
'ryo':'りょ',
'gya':'ぎゃ',
'gyu':'ぎゅ',
'gyo':'ぎょ',
'ja':'じゃ',
'ju':'じゅ',
'jo':'じょ',
'bya':'びゃ',
'byu':'びゅ',
'byo':'びょ',
'pya':'ぴゃ',
'pyu':'ぴゅ',
'pyo':'ぴょ',
#Weird characters rarely used
'di':'ぢ',
'du':'づ',
#Small Tsu
'kk':'っ',
'ss':'っ',
'tt':'っ',
'hh':'っ',
'mm':'っ',
'yy':'っ',
'rr':'っ',
'ww':'っ',
'gg':'っ',
'zz':'っ',
'dd':'っ',
'bb':'っ',
'pp':'っ',
}
Then I used the following functions
def deleteText(self):
if len(self.text) > 0:
self.textoChico = self.textoChico[:-1]
else:
self.texto = self.texto[:-1]
def writeText(self):
if len(self.text) > 3:
self.text = ""
if self.text in Table :
self.texto += Table [self.text]
self.text = ''
Upvotes: 2
Reputation: 14916
I think it comes down to your input method. Using "Anthy" IBus input on Linux PyGame works fine for inputting Hiragana text.
I suspect your keyboard is sending Latin characters, with a software driver converting it to Hiragana (etc.) but PyGame is handling the keyboard at a lower level, so is receiving the raw keypress rather than the conversion.
Maybe you can do a lookup table to convert inputted Romaji to Hiragana, but I guess this defeats the purpose of having the correct keyboard. Does the keyboard work as-desired in other applications?
"Umeboshi Font" from http://font.xxenxx.net/umeboshifont.html
Reference Code:
import pygame
# Window size
WINDOW_WIDTH = 600
WINDOW_HEIGHT = 100
WINDOW_SURFACE = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE
DARK_BLUE = ( 3, 5, 54 )
WHITE = ( 255, 255, 250 )
### initialisation
pygame.init()
pygame.mixer.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), WINDOW_SURFACE )
pygame.display.set_caption("Hiragana Test")
### For the simple text handling
#font = pygame.font.SysFont( None, 16 )
font = pygame.font.Font( 'umeboshi.ttf', 16 )
text = ''
text_surf = font.render( text, True, WHITE )
### Main Loop
clock = pygame.time.Clock()
done = False
while not done:
# Handle user-input
for event in pygame.event.get():
if ( event.type == pygame.QUIT ):
done = True
elif ( event.type == pygame.MOUSEBUTTONUP ):
# On mouse-click
pass
elif ( event.type == pygame.KEYDOWN ):
if ( event.key == pygame.K_BACKSPACE ):
text = text[:-1]
else:
text = text + event.unicode
#print( "TEXT: [%s]" % ( text ) )
text_surf = font.render( text, True, WHITE )
# Update the window, but not more than 60fps
window.fill( DARK_BLUE )
window.blit( text_surf, ( 10, WINDOW_HEIGHT//2 ) )
pygame.display.flip()
# Clamp FPS
clock.tick_busy_loop(60)
pygame.quit()
Upvotes: 1