[英]How to make keyboard Input into unicode/japanese characters?
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;例如,在我的键盘中 D = し,但是当我按下 D 键时,而不是 し,什么也没有呈现; as if the event isnt registered What should I do?好像事件未注册我该怎么办?
Note- The font is not the problem注意 - 字体不是问题
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 = ''
I think it comes down to your input method.我认为这归结为您的输入法。 Using "Anthy" IBus input on Linux PyGame works fine for inputting Hiragana text.在 Linux PyGame 上使用“Anthy”IBus 输入可以很好地输入平假名文本。
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.我怀疑您的键盘正在发送拉丁字符,软件驱动程序将其转换为平假名(等),但 PyGame 正在较低级别处理键盘,因此接收原始按键而不是转换。
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 “Umeboshi 字体”来自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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.