You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用blessed开发聊天室时textarea输入字符重复问题求助

解决blessed聊天室输入字符重复的问题

你遇到的字符重复显示问题,核心原因是重复添加输入控件+控件层级配置错误:

  • 你既给input设置了parent: screen,又调用screen.append(input),这会导致屏幕上存在两个完全一致的输入框,输入时两个控件都会渲染字符,因此出现重复。
  • 输入框未正确挂载到form容器内,引发布局和事件处理冲突。

修复方案

  1. 移除所有多余的screen.append()调用——通过parent属性指定父元素后,blessed会自动将子元素添加到父容器,无需手动重复添加。
  2. 将input的parent改为form,让输入框作为表单的一部分,避免布局冲突。
  3. 调整input的width为'100%-2',适配form的边框和left偏移,防止超出容器范围。
  4. 移除冗余的input: true配置——textarea组件本身已支持输入,该属性无需额外设置。

修改后的完整代码

const blessed = require('blessed')
const EventEmitter = require('events')

// Create a eventemitter
var chatroom = new EventEmitter()

// Create a screen object.
var screen = blessed.screen({smartCSR: true, dockBorders: true})
screen.title = 'chatroom'
screen.key(['C-c', 'escape'], () => { // exit
    return process.exit(0)
})

var chatarea = blessed.box({
    parent: screen,
    top: 0,
    title: 'chatroom',
    width: '100%',
    height: '100%-2',
    tags: true,
    border: {
        type: 'line'
    },
    style: {
        fg: 'white',
    }
})

var form = blessed.form({
    parent: screen,
    bottom: 0,
    width: '100%',
    height: 3,
    keys: true,
    border: {
        type: 'line'
    }
})

var input = blessed.textarea({
    parent: form,
    bottom: 1,
    left: 1,
    width: '100%-2',
    height: 1,
    focused: true,
    inputOnFocus: true,
    tags: true,
});
input.focus()

screen.render()

内容的提问来源于stack exchange,提问作者IsaacMak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 11:15:38