设为首页收藏本站同能贴吧 切换语言 繁体中文
开启辅助访问 切换到窄版
扫描二维码关注官方公众号
返回列表
+ 发新帖
查看: 136|回复: 0

[转载发布] Keyboard Text Input

[复制链接]
累计送礼:
0 个
累计收礼:
1 个
  • TA的每日心情
    开心
    2026-7-12 04:10
  • 签到天数: 209 天

    连续签到: 2 天

    [LV.7]常住居民III

    9071

    主题

    864

    回帖

    6万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    VIP
    7
    卡币
    58883
    OK点
    16
    推广点
    0
    同能卷
    50
    积分
    68848

    灌水之王

    发表于 2026-8-1 20:38:13 | 显示全部楼层 |阅读模式
    Text_Input 1.1
            Author: erpicci


    Introduction


            This script allows the player to insert a medium-sized text by typing directly with the keyboard. Inserted text is returned as a string.


            Requires Neon Black's Keyboard Input Script to work.


            It is completely free, both for commercial and non-commercial use. All you are asked to do is to give me credits for the script.


    Features


    • Plug and play
    • Type directly from keyboard
    • Use enter to confirm your input
    • Use backspace to delete last character
    • Use "Esc" to quit the input mode (input process will result in an empty string)
    • No known compatibility issues
    • Integrates seamlessly with window system
    • Additional window configuration (position, size, opacity)



    Screenshots
            As simple as calling a script. These shots shows how to implement an echo message and how to use other options.

    Spoiler[/url][url=https://forums.rpgmakerweb.com/attachments/echo-jpg.19364/][/url][url=https://forums.rpgmakerweb.com/attachments/04-jpg.40599/]






    How to Use


            Be sure to have Neon Black's Keyboard Input Script installed.


            Place this script in the "Materials" section of the scripts above main.


            To insert a text using an event, create a script event like:


    text_var = text_input("Window Title")


            To have a script call the text input, use:


    text_var = Scene_Text.read("Window Title")


            Only window title is mandatory. As optional arguments, you can set:


    • alignment: "center" (default), "left", "right"
    • maximum number of character: default is 104
    • left-to-right: default is true, use false to get a right-to-left input
    • configuration object (see examples)



    Examples

    Spoiler# Open a window whose title is "Talk to Bobby", text is centered,
    # allows to enter up to 20 characters, writing is right-to-left
    variable = Scene_Text.read("Talk to Bobby", "center", 20, false)

    # Open a window whose title is "Tell me something", text is aligned
    # to right, allows to enter up to 88 characters, writing is
    # left-to-right (default)
    variable = Scene_Text.read("Tell me something", "center", 88)

    # Open a window whose title is "Insert text", text is centered
    # (default), accepts up to 104 characters (default), writing is
    # left-to-right (default)
    variable = Scene_Text.read("Insert text")

    # Open a window whose title is "Text:", text is centered, allows
    # to enter up to 20 characters, writing is left-to-right,
    # window is resized, centered in the screen and semi-transparent
    config = Scene_Text_Config.new
    config.width = 256
    config.height = 128
    config.verticalAlignment = "center"
    config.horizontalAlignment = "center"
    config.opacity = 64
    variable = Scene_Text.read("Text:", "center", 20, true, config)

    # Open a window whose title is "Text:", text is centered, allows
    # to enter up to 30 characters, writing is left-to-right,
    # window is resized, aligned on the top-right corner and fully transparent
    config = Scene_Text_Config.new
    config.width = 128
    config.height = 256
    config.verticalAlignment = "top"
    config.horizontalAlignment = "right"
    config.opacity = 0
    variable = Scene_Text.read("Text:", "center", 30, true, config)

    # Open a window whose title is "Text:", text is centered, allows to enter
    # up to 20 characters, writing is left-to-right, window is partially
    # transparent
    config = Scene_Text_Config.new
    config.opacity = 160
    variable = Scene_Text.read("Text:", "center", 20, true, config)






    Script

    Spoiler                Code:       
    1. #==============================================================================
    2. # ** Text Input script
    3. #------------------------------------------------------------------------------
    4. # author:  erpicci
    5. # version: 1.1
    6. # require: CP Keyboard Input script by Neon Black
    7. #
    8. #------------------------------------------------------------------------------
    9. # * Introduction
    10. #------------------------------------------------------------------------------
    11. # This script allows the player to insert a medium-sized text by typing
    12. # directly from the keyboard. Inserted text is returned as a string.
    13. #
    14. # Requires Neon Black's Keyboard Input Script to work.
    15. #
    16. # It is completely free, both for commercial and non-commercial use. All you
    17. # are asked to do is to give me credits for the script.
    18. #
    19. #------------------------------------------------------------------------------
    20. # * Installation
    21. #------------------------------------------------------------------------------
    22. # Be sure to have Neon Black's Keyboard Input Script installed.
    23. # Place this script in the "Materials" section of the scripts above main.
    24. #
    25. # To insert a text using an event, create a script event like:
    26. #   text_var = text_input("Window Title")
    27. #
    28. # To have a script call the text input, use:
    29. #   text_var = Scene_Text.read("Window Title")
    30. #
    31. # Only window title is mandatory. As optional arguments, you can set:
    32. #  * alignment: "center" (default), "left", "right"
    33. #  * maximum number of character: default is 104
    34. #  * left-to-right: default is true, use false to get a right-to-left input
    35. #  * configuration object (see examples)
    36. #
    37. #------------------------------------------------------------------------------
    38. # * Changelog and notes
    39. #------------------------------------------------------------------------------
    40. # -1.1:   Fixed a memoty leak
    41. #
    42. # -1.1rc: Added options to manipulate the text edit window (position, size,
    43. #         opacity); manipulation is obtained through the new Scene_Text_Config
    44. #         class. Backward compatibile.
    45. #
    46. #------------------------------------------------------------------------------
    47. # * Examples
    48. #------------------------------------------------------------------------------
    49. #   # Open a window whose title is "Talk to Bobby", text is centered, allows
    50. #   # to enter up to 20 characters, writing is right-to-left
    51. #   variable = Scene_Text.read("Talk to Bobby", "center", 20, false)
    52. #
    53. #   # Open a window whose title is "Tell me something", text is aligned to
    54. #   # right, allows to enter up to 88 characters, writing is left-to-right
    55. #   # (default)
    56. #   variable = Scene_Text.read("Talk to Bobby", "center", 20)
    57. #
    58. #   # Open a window whose title is "Insert text", text is centered (default),
    59. #   # accepts up to 104 characters (default), writing is left-to-right (default)
    60. #   variable = Scene_Text.read("Talk to Bobby")
    61. #
    62. #   # Open a window whose title is "Text:", text is centered, allows
    63. #   # to enter up to 20 characters, writing is right-to-left,
    64. #   # window is resized, centered in the screen and semi-transparent
    65. #   config = Scene_Text_Config.new
    66. #   config.width = 256
    67. #   config.height = 128
    68. #   config.verticalAlignment = "center"
    69. #   config.horizontalAlignment = "center"
    70. #   config.opacity = 64
    71. #   variable = Scene_Text.read("Text:", "center", 20, true, config)
    72. #
    73. #   # Open a window whose title is "Text:", text is centered, allows
    74. #   # to enter up to 30 characters, writing is right-to-left,
    75. #   # window is resized, aligned on the top-right corner and fully transparent
    76. #   config = Scene_Text_Config.new
    77. #   config.width = 128
    78. #   config.height = 256
    79. #   config.verticalAlignment = "top"
    80. #   config.horizontalAlignment = "right"
    81. #   config.opacity = 0
    82. #   variable = Scene_Text.read("Text:", "center", 30, true, config)
    83. #
    84. #   # Open a window whose title is "Text:", text is centered, allows to enter
    85. #   # up to 20 characters, writing is left-to-right, window is partially
    86. #   # transparent
    87. #   # config = Scene_Text_Config.new
    88. #   # config.opacity = 128
    89. #   # variable = Scene_Text.read("Text:", "center", 20, true, config)
    90. #==============================================================================
    91. module Text_Input
    92.   ACCEPTED_KEYS = {
    93.     :k0 => 48, :k1 => 49, :k2 => 50, :k3 => 51, :k4 => 52,
    94.     :k5 => 53, :k6 => 54, :k7 => 55, :k8 => 56, :k9 => 57,
    95.    
    96.     :kA => 65, :kB => 66, :kC => 67, :kD => 68, :kE => 69, :kF => 70,
    97.     :kG => 71, :kH => 72, :kI => 73, :kJ => 74, :kK => 75, :kL => 76,
    98.     :kM => 77, :kN => 78, :kO => 79, :kP => 80, :kQ => 81, :kR => 82,
    99.     :kS => 83, :kT => 84, :kU => 85, :kV => 86, :kW => 87, :kX => 88,
    100.     :kY => 89, :kZ => 90,
    101.    
    102.     :kCOLON     => 186, :kQUOTE     => 222, :kSPACE      => 32,
    103.     :kCOMMA     => 188, :kPERIOD    => 190, :kSLASH      => 191,
    104.     :kBACKSLASH => 220, :kLEFTBRACE => 219, :kRIGHTBRACE => 221,
    105.     :kMINUS     => 189, :kEQUAL     => 187, :kTILDE      => 192,         
    106.   }
    107. end
    108. #==============================================================================
    109. # ** Scebe_Text_Config
    110. #------------------------------------------------------------------------------
    111. #  This object contains additional configuration for the text edit window
    112. #==============================================================================
    113. class Scene_Text_Config
    114.   attr_accessor :lines
    115.   attr_accessor :x
    116.   attr_accessor :y
    117.   attr_accessor :width
    118.   attr_accessor :height
    119.   attr_accessor :opacity
    120.   #--------------------------------------------------------------------------
    121.   # * Object Initialization: set default values
    122.   #--------------------------------------------------------------------------
    123.   def initialize
    124.     @window  = nil
    125.     @lines   = 6
    126.     @x       = 0
    127.     @y       = Graphics.height - window.fitting_height(lines)
    128.     @width   = Graphics.width
    129.     @height  = window.fitting_height(lines)
    130.     @opacity = 256
    131.     window.dispose
    132.   end
    133.   #--------------------------------------------------------------------------
    134.   # * Access to Properties of Windows
    135.   #--------------------------------------------------------------------------
    136.   def window
    137.     if (@window.nil?)
    138.       @window = Window_Base.new(0, 0, 0, 0)
    139.     end
    140.     @window
    141.   end
    142.   #--------------------------------------------------------------------------
    143.   # * Sets Horizontal Alignment
    144.   #--------------------------------------------------------------------------
    145.   def horizontalAlignment=(position = "center")
    146.     if (position == "left")
    147.       @x = 0
    148.     elsif (position == "right")
    149.       @x = Graphics.width - @width
    150.     else
    151.       @x = (Graphics.width - @width) / 2
    152.     end
    153.   end
    154.   #--------------------------------------------------------------------------
    155.   # * Sets Vertical Alignment
    156.   #--------------------------------------------------------------------------
    157.   def verticalAlignment=(position = "bottom")
    158.     if (position == "top")
    159.       @y = 0
    160.     elsif (position == "center")
    161.       @y = (Graphics.height - @height) / 2
    162.     else
    163.       @y = Graphics.height - @height
    164.     end
    165.   end
    166. end
    167. #==============================================================================
    168. # ** Scene_Text
    169. #------------------------------------------------------------------------------
    170. #  This class shows an input field for the player.
    171. #==============================================================================
    172. class Scene_Text < Scene_MenuBase
    173.   ALIGN    = "center"
    174.   MAX_CHAR = 104
    175.   LTR      = true
    176.   #--------------------------------------------------------------------------
    177.   # * Show a dialog window to the player
    178.   #--------------------------------------------------------------------------
    179.   def self.read(title, align = ALIGN, max_char = MAX_CHAR, ltr = LTR, opts = nil)
    180.     @@text     = ""
    181.     @@title    = title
    182.     @@align    = align
    183.     @@max_char = max_char
    184.     @@ltr      = ltr
    185.     @@opts     = opts.nil? ? Scene_Text_Config.new : opts
    186.     SceneManager.call(Scene_Text)
    187.     Fiber.yield while SceneManager.scene_is?(Scene_Text)
    188.     return @@text
    189.   end
    190.   #--------------------------------------------------------------------------
    191.   # * Start Processing
    192.   #--------------------------------------------------------------------------
    193.   def start
    194.     super
    195.     @edit_window  = Window_TextEdit.new(@@title, @@align, @@max_char, @@ltr,
    196.       @@opts)
    197.     @edit_window.set_handler(:ok, method(:on_input_ok))
    198.   end
    199.   #--------------------------------------------------------------------------
    200.   # * Set text when done
    201.   #--------------------------------------------------------------------------
    202.   def on_input_ok
    203.     @@text = @edit_window.text
    204.     @@opts.window.dispose
    205.     return_scene
    206.   end
    207. end
    208. #==============================================================================
    209. # ** Window_TextEdit
    210. #------------------------------------------------------------------------------
    211. #  This window allows to edit a text.
    212. #==============================================================================
    213. class Window_TextEdit < Window_Selectable
    214.   include Text_Input
    215.   #--------------------------------------------------------------------------
    216.   # * Public Instance Variables
    217.   #--------------------------------------------------------------------------
    218.   attr_accessor :text
    219.   #--------------------------------------------------------------------------
    220.   # * Object Initialization
    221.   #--------------------------------------------------------------------------
    222.   def initialize(title, align = "center", max_char = 104, ltr = true, opts = nil)
    223.     opts = opts.nil? ? Scene_Text_Config.new : opts
    224.     super(opts.x, opts.y, opts.width, opts.height)
    225.     self.opacity = opts.opacity
    226.    
    227.     @text     = ""
    228.     @title    = title
    229.     @align    = align
    230.     @max_char = max_char
    231.     @ltr      = ltr
    232.     @opts     = opts
    233.     @index    = @text.size
    234.     activate
    235.     refresh
    236.   end
    237.   #--------------------------------------------------------------------------
    238.   # * Revert to Default Text
    239.   #--------------------------------------------------------------------------
    240.   def restore_default
    241.     @text = ""
    242.     @index = @text.size
    243.     refresh
    244.     return !@text.empty?
    245.   end
    246.   #--------------------------------------------------------------------------
    247.   # * Get Character Width
    248.   #--------------------------------------------------------------------------
    249.   def char_width
    250.     text_size($game_system.japanese? ? "‚ " : "A").width
    251.   end
    252.   #--------------------------------------------------------------------------
    253.   # * Get Number of Columns
    254.   #--------------------------------------------------------------------------
    255.   def max_col
    256.     [(@opts.width - 32) / char_width, @max_char].min
    257.   end
    258.   #--------------------------------------------------------------------------
    259.   # * Get Left Padding
    260.   #--------------------------------------------------------------------------
    261.   def left(n)
    262.     return 10                                if @align == "left"
    263.     return (width - 32 - n * char_width)     if @align == "right"
    264.     return (width - 32 - n * char_width) / 2 # if align == "center"
    265.   end
    266.   #--------------------------------------------------------------------------
    267.   # * Get Rectangle for Displaying Item
    268.   #--------------------------------------------------------------------------
    269.   def item_rect(index)
    270.     index -= 1 if index == @max_char
    271.     x = index % max_col
    272.     y = index / max_col
    273.     n = [(@max_char - y * max_col), max_col].min
    274.    
    275.     x = left(n) + x * char_width
    276.     x = width   - x - 48 if not @ltr
    277.     y = 24      + y * (line_height + 4)
    278.     Rect.new(x, y, char_width, line_height)
    279.   end
    280.   #--------------------------------------------------------------------------
    281.   # * Get Underline Rectangle
    282.   #--------------------------------------------------------------------------
    283.   def underline_rect(index)
    284.     rect = item_rect(index)
    285.     rect.x += 1
    286.     rect.y += rect.height
    287.     rect.width -= 2
    288.     rect.height = 2
    289.     rect
    290.   end
    291.   #--------------------------------------------------------------------------
    292.   # * Get Underline Color
    293.   #--------------------------------------------------------------------------
    294.   def underline_color
    295.     color = normal_color
    296.     color.alpha = 48
    297.     color
    298.   end
    299.   #--------------------------------------------------------------------------
    300.   # * Draw Underline
    301.   #--------------------------------------------------------------------------
    302.   def draw_underline(index)
    303.     contents.fill_rect(underline_rect(index), underline_color)
    304.   end
    305.   #--------------------------------------------------------------------------
    306.   # * Draw Text
    307.   #--------------------------------------------------------------------------
    308.   def draw_char(index)
    309.     rect       = item_rect(index)
    310.     rect.x     += 4
    311.     rect.width += 4
    312.     change_color(normal_color)
    313.     draw_text(rect, @text[index] || "")
    314.   end
    315.   #--------------------------------------------------------------------------
    316.   # * Draw Title
    317.   #--------------------------------------------------------------------------
    318.   def draw_title
    319.     draw_text(0, 0, self.width, line_height, @title, 1)
    320.   end
    321.   #--------------------------------------------------------------------------
    322.   # * Refresh
    323.   #--------------------------------------------------------------------------
    324.   def refresh
    325.     contents.clear
    326.     draw_title
    327.     @text.size.times {|i| draw_char(i) }
    328.     @max_char.times  {|i| draw_underline(i) }
    329.     cursor_rect.set(item_rect(@index))
    330.   end
    331.   #--------------------------------------------------------------------------
    332.   # * Handle Process
    333.   #--------------------------------------------------------------------------
    334.   def process_handling
    335.     return unless open? && active
    336.     process_delete if Input.repeat?(:kBACKSPACE)
    337.     process_abort  if Input.trigger?(:kESC)
    338.     process_ok     if Input.trigger?(:kENTER)
    339.     process_keyboard
    340.   end
    341.   #--------------------------------------------------------------------------
    342.   # * Check Input From Keyboard
    343.   #--------------------------------------------------------------------------
    344.   def process_keyboard
    345.     ACCEPTED_KEYS.each {|key|
    346.       if Input.repeat?(key[0])
    347.         c = (key[0] != :kSPACE) ? Keyboard.add_char(Ascii::SYM[key[0]]) : " "
    348.         process_add(c)
    349.         Sound.play_ok
    350.       end
    351.     }
    352.   end
    353.   #--------------------------------------------------------------------------
    354.   # * Add One Character
    355.   #--------------------------------------------------------------------------
    356.   def process_add(c)
    357.     return if @index > @max_char
    358.     if @index == @max_char
    359.       @text[@index - 1] = c
    360.     else
    361.       @text  += c
    362.       @index += 1
    363.     end
    364.     refresh
    365.   end
    366.   #--------------------------------------------------------------------------
    367.   # * Delete One Character
    368.   #--------------------------------------------------------------------------
    369.   def process_delete
    370.     return if @index == 0
    371.     @index -= 1
    372.     @text  = @text[0, @index]
    373.     Sound.play_cancel
    374.     refresh
    375.   end
    376.   #--------------------------------------------------------------------------
    377.   # * Abort Text Input
    378.   #--------------------------------------------------------------------------
    379.   def process_abort
    380.     restore_default
    381.     Sound.play_cancel
    382.     call_ok_handler
    383.   end
    384.   #--------------------------------------------------------------------------
    385.   # * Complete Text Input
    386.   #--------------------------------------------------------------------------
    387.   def process_ok
    388.     Sound.play_ok
    389.     call_ok_handler
    390.   end
    391. end
    392. #==============================================================================
    393. # ** Game_Interpreter
    394. #------------------------------------------------------------------------------
    395. #  An interpreter for executing event commands. This class is used within the
    396. # Game_Map, Game_Troop, and Game_Event classes.
    397. #==============================================================================
    398. class Game_Interpreter
    399.   #--------------------------------------------------------------------------
    400.   # * Insert a text
    401.   #--------------------------------------------------------------------------
    402.   def text_input(title, align = "center", max_char = 103, ltr = true, opts = nil)
    403.     Scene_Text.read(title, align, max_char, ltr, opts)
    404.   end
    405. end
    复制代码






            This script is also available on PasteBin: http://pastebin.com/dh16PCaj.


    Changelog:

    Spoiler

    • 1.1 [http://pastebin.com/HST0A6bJ] Fixed a memory leak: windows did not get properly disposed. Backward compatible.
    • 1.1rc [http://pastebin.com/dh16PCaj] Added options to manipulate the text edit window (position, size, opacity); manipulation is obtained through the new Scene_Text_Config class. Backward compatibile. Activating the scene with no additional config object falls back to previous version.
    • 1.0 [http://pastebin.com/TYtbDDPy] Initial release.







    FAQ


            Q: How do I store the inserted text into a game variable?


            A: See this comment.


            Q: How do I insert newlines?


            A: This is currently not supported. A workaround is explained in this comment.


            Q: How do I check inserted text to prepare a response?


            A: You can use the script to store inserted text into a variable, then you can perform any check you like. For an example, see this comment.


            Q: How can I change windows size, position and opacity?


            A: Be sure to have version 1.1rc (or later) installed, then check examples in this page.




    Credit and Thanks
            - erpicci, author
            - Neon Black, for his Keyboard Input Script
            - Tsukihime, for his Simple Text Input script, which I studied to make my own


            - Sixth, for spotting a memory leak in version <1.1 and suggesting how to fix


    本贴来自国际rpgmaker官方论坛作者:erpicci处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:https://forums.rpgmakerweb.com/threads/keyboard-text-input.36828/

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    x
    天天去同能,天天有童年!
    回复 送礼论坛版权

    使用道具 举报

    文明发言,和谐互动
    文明发言,和谐互动
    高级模式
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    简体中文
    繁體中文
    English(英语)
    日本語(日语)
    Deutsch(德语)
    Русский язык(俄语)
    بالعربية(阿拉伯语)
    Türkçe(土耳其语)
    Português(葡萄牙语)
    ภาษาไทย(泰国语)
    한어(朝鲜语/韩语)
    Français(法语)
    关闭

    幸运抽奖

    社区每日抽奖来袭,快来试试你是欧皇还是非酋~

    立即查看

    聊天机器人
    Loading...

    QQ|Archiver|手机版|小黑屋|同能RPG制作大师 ( 沪ICP备12027754号-3 )

    GMT+8, 2026-8-17 13:37 , Processed in 0.144229 second(s), 52 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

    快速回复 返回顶部 返回列表