Vue TextArea - Handle the Keyboard Events

The TextArea raises four keyboard events: keyDown, keyUp and enterKey. Within the functions that handle them, you can access the original keyboard events. If you are not going to change the functions during the lifetime of the UI component, assign them to the 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>

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