Vue TextArea - Handle the Keyboard Events

The TextArea raises three keyboard events: keyDown, keyUp and enterKey. Within functions that handle them, you can access original keyboard events. If you are not going to change functions during the lifetime of the UI component, assign them to respective UI component properties.

  • <template>
  • <DxTextArea
  • @key-down="onKeyDown"
  • @key-up="onKeyUp"
  • @enter-key="onEnterKey"
  • />
  • </template>
  •  
  • <script>
  • import 'devextreme/dist/css/dx.light.css';
  • import { DxTextArea } from 'devextreme-vue/text-area';
  •  
  • export default {
  • components: {
  • DxTextArea
  • },
  • methods: {
  • onKeyDown(e) {
  • const keyCode = e.event.key;
  • // Event handling commands go here
  • },
  • onKeyUp(e) {
  • const keyCode = e.event.key;
  • // Event handling commands go here
  • },
  • onEnterKey(e) {
  • // Event handling commands go here
  • }
  • }
  • }
  • </script>
NOTE
You can also use the input event. This is not a strictly keyboard event, a mouse action can also change a TextArea input value.

Use the registerKeyHandler(key, handler) method to implement a custom handler for a key.

  • <template>
  • <DxTextArea :ref="myTextAreaRef" />
  • </template>
  • <script>
  • import 'devextreme/dist/css/dx.light.css';
  •  
  • import DxTextArea from 'devextreme-vue/text-area';
  •  
  • const myTextAreaRef = 'my-text-area';
  •  
  • export default {
  • components: {
  • DxTextArea
  • },
  • data() {
  • return {
  • myTextAreaRef
  • }
  • },
  • computed: {
  • textArea: function() {
  • return this.$refs[myTextAreaRef].instance;
  • }
  • },
  • mounted: function() {
  • this.textArea.registerKeyHandler('backspace', function(e) {
  • // The argument "e" contains information on the event
  • });
  • this.textArea.registerKeyHandler('space', function(e) {
  • // ...
  • });
  • }
  • }
  • </script>
See Also