为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

SMILET 2024-01-15 15:20:31
我有以下功能 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 键时,没有渲染し,而是没有渲染任何内容;好像该活动未注册 我该怎么办?注意-字体不是问题
查看完整描述

2 回答

?
杨__羊羊

TA贡献1943条经验 获得超7个赞

我是这样解决的:


我只是用字典将字符转换为平假名


 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 = ''


查看完整回答
反对 回复 2024-01-15
?
幕布斯6054654

TA贡献1876条经验 获得超7个赞

我认为这取决于你的输入法。在 Linux PyGame 上使用“Anthy”IBus 输入可以很好地输入平假名文本。

怀疑您的键盘正在发送拉丁字符,并通过软件驱动程序将其转换为平假名(等),但 PyGame 正在较低级别处理键盘,因此接收原始按键而不是转换。

参考代码:

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


查看完整回答
反对 回复 2024-01-15
  • 2 回答
  • 0 关注
  • 40 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信