That's the right approach, but you should not use hex constants as in Niuranga's code. The JavaDoc for KeyEvent has the following warning:
WARNING: Aside from those keys that are defined by the Java language (VK_ENTER, VK_BACK_SPACE, and VK_TAB), do not rely on the values of the VK_ constants. Sun reserves the right to change these values as needed to accomodate a wider range of keyboards in the future.
KeyEvent includes public constants for all the virtual keys, eg VK_BACK_SPACE, VK_DELETE , etc and these are what you should use.