如何添加onKeyPress事件以响应Material-UI Textfield? [英] How can I add onKeyPress event to react material-ui textfield?

查看:4
本文介绍了如何添加onKeyPress事件以响应Material-UI Textfield?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了Reaction Material-UI中的TextField。我想知道用户是否按了Ctrl+Enter。我已尝试使用onKeyPress事件,但没有结果。我如何实现这一点?

<TextField
    value={this.state.message}
    autoFocus={true}
    hintText='Type your message here'
    onChange={this.onChangeMessage}
    onKeyPress={(event) => {
        if (event.ctrlKey && event.keyCode == '13')
            this.sendMessage();
    }}
    multiLine={true}
/>

推荐答案

onKeyPress是如上所述的反应支持的合成键事件。尝试此代码:

 onKeyPress= {(e) => {
            if (e.key === 'Enter') {
              console.log('Enter key pressed');
              // write your functionality here
            }
    }}

这篇关于如何添加onKeyPress事件以响应Material-UI Textfield?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆