React TextBox - Handle the Keyboard Events

The TextBox 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.

jQuery
JavaScript
$(function() {
    $("#textBoxContainer").dxTextBox({
        onKeyDown: function (e) {
            const keyCode = e.event.key;
            // Event handling commands go here
        },
        onKeyUp: function (e) {
            const keyCode = e.event.key;
            // Event handling commands go here
        },
        onEnterKey: function (e) {
            // Event handling commands go here
        }
    });
});
Angular
HTML
TypeScript
<dx-text-box
    (onKeyDown)="onKeyDown($event)"
    (onKeyUp)="onKeyUp($event)"
    (onEnterKey)="onEnterKey($event)">
</dx-text-box>
import { DxTextBoxModule } from "devextreme-angular";
// ...
export class AppComponent {
    onKeyDown (e) {
        let keyCode = e.event.key;
        // Event handling commands go here
    }
    onKeyUp (e) {
        let keyCode = e.event.key;
        // Event handling commands go here
    }
    onEnterKey (e) {
        // Event handling commands go here
    }
}
@NgModule({
    imports: [
        // ...
        DxTextBoxModule
    ],
    // ...
})
Vue
<template>
    <DxTextBox
        @key-down="onKeyDown"
        @key-up="onKeyUp"
        @enter-key="onEnterKey"
    />
</template>

<script>
import 'devextreme/dist/css/dx.light.css';

import { DxTextBox } from 'devextreme-vue/text-box';

export default {
    components: {
        DxTextBox
    },
    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>
React
import React from 'react';
import 'devextreme/dist/css/dx.light.css';

import { TextBox } from 'devextreme-react/text-box';

class App extends React.Component {
    render() {
        return (
            <TextBox
                onKeyDown={this.onKeyDown}
                onKeyUp={this.onKeyUp}
                onEnterKey={this.onEnterKey}
            />
        );
    }

    onKeyDown(e) {
        let keyCode = e.event.key;
        // Event handling commands go here
    }
    onKeyUp(e) {
        let keyCode = e.event.key;
        // Event handling commands go here
    }
    onEnterKey(e) {
        // Event handling commands go here
    }
}

export default App;

If you are going to change the handling functions at runtime, or if you need to attach several functions to a single event, use the on(eventName, eventHandler) method. This approach is more typical of jQuery.

JavaScript
const keyDownHandler1 = function (e) {
    const keyCode = e.event.key;
    // First handler of the "keyDown" event
};

const keyDownHandler2 = function (e) {
    const keyCode = e.event.key;
    // Second handler of the "keyDown" event
};

$("#textBoxContainer").dxTextBox("instance")
    .on("keyDown", keyDownHandler1)
    .on("keyDown", keyDownHandler2);

You can implement a custom handler for a key using the registerKeyHandler(key, handler) method.

jQuery
JavaScript
function registerKeyHandlers () {
    const textBox = $("#textBoxContainer").dxTextBox("instance");
    textBox.registerKeyHandler("backspace", function(e) {
        // The argument "e" contains information on the event
    });
    textBox.registerKeyHandler("space", function(e) {
        // ...
    });
}
Angular
TypeScript
import { ..., ViewChild, AfterViewInit } from '@angular/core';
import { DxTextBoxModule, DxTextBoxComponent } from 'devextreme-angular';
// ...
export class AppComponent implements AfterViewInit {
    @ViewChild(DxTextBoxComponent, { static: false }) textBox: DxTextBoxComponent;
    // Prior to Angular 8
    // @ViewChild(DxTextBoxComponent) textBox: DxTextBoxComponent;

    ngAfterViewInit () {
        this.textBox.instance.registerKeyHandler('backspace', function(e) {
            // The argument "e" contains information on the event
        });
        this.textBox.instance.registerKeyHandler('space', function(e) {
            // ...
        });
    }
}
@NgModule({
    imports: [
        // ...
        DxTextBoxModule
    ],
    // ...
})
Vue
<template>
    <DxTextBox :ref="myTextBoxRef" />
</template>
<script>
import 'devextreme/dist/css/dx.light.css';

import DxTextBox from 'devextreme-vue/text-box';

const myTextBoxRef = 'my-text-box';

export default {
    components: {
        DxTextBox
    },
    data() {
        return {
            myTextBoxRef
        }
    },
    computed: {
        textBox: function() {
            return this.$refs[myTextBoxRef].instance;
        }
    },
    mounted: function() {
        this.textBox.registerKeyHandler('backspace', function(e) {
            // The argument "e" contains information on the event
        });
        this.textBox.registerKeyHandler('space', function(e) {
            // ...
        });
    }
}
</script>
React
App.js
import React from 'react';
import 'devextreme/dist/css/dx.light.css';

import { TextBox } from 'devextreme-react/text-box';

class App extends React.Component {
    constructor(props) {
        super(props);

        this.textBoxRef = React.createRef();
    }

    render() {
        return (
            <TextBox ref={this.textBoxRef} />
        );
    }

    get textBox() {
        return this.textBoxRef.current.instance;
    }

    componentDidMount() {
        this.textBox.registerKeyHandler('backspace', function(e) {
            // The argument "e" contains information on the event
        });
        this.textBox.registerKeyHandler('space', function(e) {
            // ...
        });
    }
}

export default App;
See Also