訂製按鍵映射 link

配置項 config.keymap 中包含一個事件名稱與觸發事件的keysym的映射關係。

Note

許多用戶已經學會了Ren’Py的默認按鍵綁定,並希望各遊戲保持一致。

在Ren’Py的keysym系統中,使用字串表示滑鼠按鍵、遊戲手把按鍵和鍵盤按鍵。

滑鼠按鍵的keysym格式是‘mouseup_#’或‘mousedown_#’,其中的 # 符號代表按鍵的編號。Ren’Py假設滑鼠有5個按鍵,其中按鍵1、2、3分別表示左、中、右鍵,按鍵4和5分別表示滾輪的上滑和下滑。例如,“mousedown_1”代表滑鼠左鍵按下,“mouseup_1”表示滑鼠左鍵彈起,“mousedown_4”表示滾輪上滑。

總共有兩種鍵盤的keysym。第一種是字串裡只有一個字元,有某個按鍵被按下時生成。通常用於綁定字母或數字按鍵。典型的keysym包括“a”、“A”和“7”等。 注意這裡的字母對大小寫敏感,“a”與“A”是不同字母。 這種keysym只對能生成文本的事件有用——例如,按鍵釋放時無法匹配到 keyup_a,因為沒有產生文本。

鍵盤keysym也可以是符號或者功能按鍵。可以是pygame.constants中定義的任意 K_ 形式常量。這種keysym字串類似於 “K_BACKSPACE”、 “K_RETURN”和 “K_TAB”;完整的keysym定義詳見 這裡

鍵盤和滑鼠keysym可以使用下列前綴,與後面的字元用下劃線分割:

alt

當alt鍵被同時按下時匹配。不同時按下alt鍵的keysym與不帶前綴的匹配。

meta

當meta、command或windows鍵被同時按下時匹配。不同時按下那些鍵的keysym與不帶前綴的匹配。

ctrl

當ctrl鍵被同時按下時匹配。不同時按下ctrl鍵的keysym與不帶前綴的匹配。(ctrl鍵很少用,因為它通常會觸發跳過。)

osctrl

針對Mac系統的alt鍵,和其他系統的ctrl鍵。

shift

當shift鍵被同時按下時匹配。

noshift

當shift鍵沒有被按下時匹配。一個 K_ 形式keysym忽略shift按鍵狀態。

caps

當Caps Lock鍵處於On狀態時匹配。

nocaps

當Caps Lock鍵處於Off狀態時匹配。

num

當Num Lock鍵處於On狀態時匹配。

nonum

當Num Lock鍵處於Off狀態時匹配。

repeat

按鍵長按狀態時,則匹配為repeat。不能長按的keysym或者不帶這個前綴的keysym不會匹配到repeat。(滑鼠按鍵無效)

any

匹配任意按鍵的按下或持續按下狀態。

keydown

默認情況下,匹配按鍵被按下狀態。

keyup

匹配按鍵被釋放狀態。

例如,keysym “shift_alt_K_F5”,當shift和alt鍵長按時,再按下F5可以匹配到。

要修改keysym和事件的綁定關係,就需要修改 config.keymap。下面的腳本將“t”鍵添加到按鍵列表中,作用是dismiss某say語句,並從列表中移除了空格鍵。

init python:
    config.keymap['dismiss'].append('K_t')
    config.keymap['dismiss'].remove('K_SPACE')

預設的按鍵映射放在renpy/common/00keymap.rpy文件中,下面是8.1.0版本的配置:

config.keymap = dict(

    # 除非明確禁用,各處都能使用的綁定快捷鍵。
    rollback = [ 'any_K_PAGEUP', 'any_KP_PAGEUP', 'K_AC_BACK', 'mousedown_4' ],
    screenshot = [ 'alt_K_s', 'alt_shift_K_s', 'noshift_K_s' ],
    toggle_afm = [ ],
    toggle_fullscreen = [ 'alt_K_RETURN', 'alt_K_KP_ENTER', 'K_F11', 'noshift_K_f' ],
    game_menu = [ 'K_ESCAPE', 'K_MENU', 'K_PAUSE', 'mouseup_3' ],
    hide_windows = [ 'mouseup_2', 'noshift_K_h' ],
    launch_editor = [ 'shift_K_e' ],
    dump_styles = [ ],
    reload_game = [ 'alt_K_r', 'shift_K_r' ],
    inspector = [ 'alt_K_i', 'shift_K_i' ],
    full_inspector = [ 'alt_shift_K_i' ],
    developer = [ 'alt_K_d', 'shift_K_d', ],
    quit = [ ],
    iconify = [ ],
    help = [ 'K_F1', 'meta_shift_/' ],
    choose_renderer = ['alt_K_g', 'shift_K_g' ],
    progress_screen = [ 'alt_shift_K_p', 'meta_shift_K_p', 'K_F2' ],
    accessibility = [ 'K_a' ],

    # 數據讀取。
    self_voicing = [ 'alt_K_v', 'K_v' ],
    clipboard_voicing = [ 'alt_shift_K_c', 'shift_K_c' ],
    debug_voicing = [ 'alt_shift_K_v', 'meta_shift_K_v' ],

    # say相關。
    rollforward = [ 'any_K_PAGEDOWN', 'any_KP_PAGEDOWN', 'mousedown_5', ],
    dismiss = [ 'K_RETURN', 'K_SPACE', 'K_KP_ENTER', 'K_SELECT', 'mouseup_1' ],
    dismiss_unfocused = [ ],

    # 暫停。
    dismiss_hard_pause = [ ],

    # 焦點相關。
    focus_left = [ 'any_K_LEFT', 'any_KP_LEFT' ],
    focus_right = [ 'any_K_RIGHT', 'any_KP_RIGHT' ],
    focus_up = [ 'any_K_UP', 'any_KP_UP' ],
    focus_down = [ 'any_K_DOWN', 'any_KP_DOWN' ],

    # 按鈕。
    button_ignore = [ 'mousedown_1' ],
    button_select = [ 'K_RETURN', 'K_KP_ENTER', 'K_SELECT', 'mouseup_1',  ],
    button_alternate = [ 'mouseup_3' ],
    button_alternate_ignore = [ 'mousedown_3' ],

    # 輸入。
    input_backspace = [ 'any_K_BACKSPACE' ],
    input_enter = [ 'K_RETURN', 'K_KP_ENTER' ],
    input_next_line = [ 'shift_K_RETURN', 'shift_K_KP_ENTER' ],
    input_left = [ 'any_K_LEFT', 'any_KP_LEFT' ],
    input_right = [ 'any_K_RIGHT', 'any_KP_RIGHT' ],
    input_up = [ 'any_K_UP', 'any_KP_UP' ],
    input_down = [ 'any_K_DOWN', 'any_KP_DOWN' ],
    input_delete = [ 'any_K_DELETE', 'any_KP_DELETE' ],
    input_home = [ 'K_HOME', 'KP_HOME', 'meta_K_LEFT' ],
    input_end = [ 'K_END', 'KP_END', 'meta_K_RIGHT' ],
    input_copy = [ 'ctrl_noshift_K_INSERT', 'ctrl_noshift_K_c', 'meta_noshift_K_c' ],
    input_paste = [ 'shift_K_INSERT', 'ctrl_noshift_K_v', 'meta_noshift_K_v' ],
    input_jump_word_left = [ 'osctrl_K_LEFT', 'osctrl_KP_LEFT' ],
    input_jump_word_right = [ 'osctrl_K_RIGHT', 'osctrl_KP_RIGHT' ],
    input_delete_word = [ 'osctrl_K_BACKSPACE' ],
    input_delete_full = [ 'meta_K_BACKSPACE' ],

    # 視口。
    viewport_leftarrow = [ 'any_K_LEFT', 'any_KP_LEFT' ],
    viewport_rightarrow = [ 'any_K_RIGHT', 'any_KP_RIGHT' ],
    viewport_uparrow = [ 'any_K_UP', 'any_KP_UP' ],
    viewport_downarrow = [ 'any_K_DOWN', 'any_KP_DOWN' ],
    viewport_wheelup = [ 'mousedown_4' ],
    viewport_wheeldown = [ 'mousedown_5' ],
    viewport_drag_start = [ 'mousedown_1' ],
    viewport_drag_end = [ 'mouseup_1' ],
    viewport_pageup = [ 'any_K_PAGEUP', 'any_KP_PAGEUP'],
    viewport_pagedown = [ 'any_K_PAGEDOWN', 'any_KP_PAGEDOWN' ],

    # 這些按鍵控制跳過。
    skip = [ 'K_LCTRL', 'K_RCTRL' ],
    stop_skipping = [ ],
    toggle_skip = [ 'K_TAB' ],
    fast_skip = [ '>', 'shift_K_PERIOD' ],

    # Bar。
    bar_activate = [ 'mousedown_1', 'K_RETURN', 'K_KP_ENTER', 'K_SELECT' ],
    bar_deactivate = [ 'mouseup_1', 'K_RETURN', 'K_KP_ENTER', 'K_SELECT' ],
    bar_left = [ 'any_K_LEFT', 'any_KP_LEFT' ],
    bar_right = [ 'any_K_RIGHT', 'any_KP_RIGHT' ],
    bar_up = [ 'any_K_UP', 'any_KP_UP' ],
    bar_down = [ 'any_K_DOWN', 'any_KP_DOWN' ],


    # 刪除存檔。
    save_delete = [ 'K_DELETE', 'KP_DELETE' ],

    # 可拖拽組件。
    drag_activate = [ 'mousedown_1' ],
    drag_deactivate = [ 'mouseup_1' ],

    # 除錯控制台。
    console = [ 'shift_K_o', 'alt_shift_K_o' ],
    console_older = [ 'any_K_UP', 'any_KP_UP' ],
    console_newer = [ 'any_K_DOWN', 'any_KP_DOWN' ],

    # 編導器
    director = [ 'noshift_K_d' ],

    # 忽略(保持後向相容)。
    toggle_music = [ ],
    viewport_up = [ ],
    viewport_down = [ ],

    # Profile命令。
    performance = [ 'K_F3' ],
    image_load_log = [ 'K_F4' ],
    profile_once = [ 'K_F8' ],
    memory_profile = [ 'K_F7' ],

    )

手把的綁定工作會有一點不同。手把綁定會將一個事件映射為一個或多個Ren’Py事件名。預設的手把綁定設置如下:

config.pad_bindings = {
    "pad_leftshoulder_press" : [ "rollback", ],
    "pad_lefttrigger_pos" : [ "rollback", ],
    "pad_back_press" : [ "rollback", ],

    "repeat_pad_leftshoulder_press" : [ "rollback", ],
    "repeat_pad_lefttrigger_pos" : [ "rollback", ],
    "repeat_pad_back_press" : [ "rollback", ],

    "pad_guide_press" : [ "game_menu", ],
    "pad_start_press" : [ "game_menu", ],

    "pad_y_press" : [ "hide_windows", ],

    "pad_rightshoulder_press" : [ "rollforward", ],
    "repeat_pad_rightshoulder_press" : [ "rollforward", ],

    "pad_righttrigger_pos" : [ "dismiss", "button_select", "bar_activate", "bar_deactivate" ],
    "pad_a_press" : [ "dismiss", "button_select", "bar_activate", "bar_deactivate"],
    "pad_b_press" : [ "button_alternate" ],

    "pad_dpleft_press" : [ "focus_left", "bar_left", "viewport_leftarrow" ],
    "pad_leftx_neg" : [ "focus_left", "bar_left", "viewport_leftarrow" ],
    "pad_rightx_neg" : [ "focus_left", "bar_left", "viewport_leftarrow" ],

    "pad_dpright_press" : [ "focus_right", "bar_right", "viewport_rightarrow" ],
    "pad_leftx_pos" : [ "focus_right", "bar_right", "viewport_rightarrow" ],
    "pad_rightx_pos" : [ "focus_right", "bar_right", "viewport_rightarrow" ],

    "pad_dpup_press" : [ "focus_up", "bar_up", "viewport_uparrow" ],
    "pad_lefty_neg" : [ "focus_up", "bar_up", "viewport_uparrow" ],
    "pad_righty_neg" : [ "focus_up", "bar_up", "viewport_uparrow" ],

    "pad_dpdown_press" : [ "focus_down", "bar_down", "viewport_downarrow" ],
    "pad_lefty_pos" : [ "focus_down", "bar_down", "viewport_downarrow" ],
    "pad_righty_pos" : [ "focus_down", "bar_down", "viewport_downarrow" ],

    "repeat_pad_dpleft_press" : [ "focus_left", "bar_left", "viewport_leftarrow" ],
    "repeat_pad_leftx_neg" : [ "focus_left", "bar_left", "viewport_leftarrow" ],
    "repeat_pad_rightx_neg" : [ "focus_left", "bar_left", "viewport_leftarrow" ],

    "repeat_pad_dpright_press" : [ "focus_right", "bar_right", "viewport_rightarrow" ],
    "repeat_pad_leftx_pos" : [ "focus_right", "bar_right", "viewport_rightarrow" ],
    "repeat_pad_rightx_pos" : [ "focus_right", "bar_right", "viewport_rightarrow" ],

    "repeat_pad_dpup_press" : [ "focus_up", "bar_up", "viewport_uparrow" ],
    "repeat_pad_lefty_neg" : [ "focus_up", "bar_up", "viewport_uparrow" ],
    "repeat_pad_righty_neg" : [ "focus_up", "bar_up", "viewport_uparrow" ],

    "repeat_pad_dpdown_press" : [ "focus_down", "bar_down", "viewport_downarrow" ],
    "repeat_pad_lefty_pos" : [ "focus_down", "bar_down", "viewport_downarrow" ],
    "repeat_pad_righty_pos" : [ "focus_down", "bar_down", "viewport_downarrow" ],
}

手把按鍵的事件名格式是“pad_*button*_press”和“pad_*button*_release”。 模擬搖杆事件格式是“pad_*axis*_pos”、“pad_*axis*_neg”和“pad_*axis*_zero”。 持續按住某個按鍵時,遊戲手把會生成另一個事件,前綴為“repeat_”。

可以不執行特定初始流程就能使用手把,但預設禁用該項功能。 任天堂Switch的Pro等手把,在電腦上使用時會要求特殊的初始化流程。 控制器黑名單詳見 config.controller_blocklist