如何将键盘输入变成unicode/日文字符?

我有以下功能


 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()

基本上,它允许我输入一些内容,然后它就会呈现在屏幕上。问题是,当我打开日语键盘并开始打字时,它不显示日语文本。相反,它只是显示罗马字母


例如,在我的键盘中 D = し,但是当我按下 D 键时,没有渲染し,而是没有渲染任何内容;好像该活动未注册 我该怎么办?


注意-字体不是问题


SMILET
浏览 46回答 2
2回答

杨__羊羊

我是这样解决的:我只是用字典将字符转换为平假名 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':'っ',}然后我使用了以下函数   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 = ''

幕布斯6054654

我认为这取决于你的输入法。在 Linux PyGame 上使用“Anthy”IBus 输入可以很好地输入平假名文本。我怀疑您的键盘正在发送拉丁字符,并通过软件驱动程序将其转换为平假名(等),但 PyGame 正在较低级别处理键盘,因此接收原始按键而不是转换。参考代码:import pygame# Window sizeWINDOW_WIDTH    = 600WINDOW_HEIGHT   = 100WINDOW_SURFACE  = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLEDARK_BLUE = (   3,   5,  54 )WHITE     = ( 255, 255, 250 )### initialisationpygame.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 Loopclock = pygame.time.Clock()done = Falsewhile 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()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python