import java.awt.event.KeyEvent; public class KeyEventExample { public static void main(String[] args) { int keyCode = KeyEvent.VK_F1; String keyText = KeyEvent.getKeyText(keyCode); System.out.println(keyText); } }
import java.awt.event.KeyEvent; public class KeyEventExample { public static void main(String[] args) { String keyText = "S"; int keyCode = KeyEvent.getExtendedKeyCodeForChar(keyText.charAt(0)); System.out.println(keyCode); } }Output: 83 This example shows how to get the key code of a character using the getExtendedKeyCodeForChar() method. Here, we have passed the character 'S' and printed its key code. Note that we are using the charAt() method to get the first character of the string. Package/Library: java.awt.event