KEY_TYPED metodi
KEY_TYPED: Bu voqea bir joyda KEY_PRESSED va KEY_TYPED
hodisalari. Hech qachon bosilgan haqiqiy tugmaga mos keladigan keyCode()
xususiyatini o'z ichiga olmaydi va getKeyCode () ushbu turdagi hodisaga
chaqirilganda 0 qaytariladi. Unicode vakili bo'lmagan kalitlarda (masalan, PAGE
UP va PRINT SCREEN ) hech qanday KEY_TYPED hodisasi yaratilmaydi .
Klaviatura hodisalari bilan bog'liq bo'lgan bir nechta yangi InputEvent
modifikatorlari
mavjud:
SHIFT_DOWN_MASK,
CTRL_DOWN_MASK,
META_DOWN_MASK, ALT_DOWN_MASK, ALT_GRAPH_DOWN_MASK.
Kengaytirilgan modifikatorlarni qaytarib olish uchun ikkita yangi API bor :
getModifiersEx () va getModifiersExText (), bu bir vaqtning o'zida bir nechta
tugmachalarning pasayishi bo'lgan holatlar bilan ishlashga imkon beradi.
Bir necha daqiqadan ko'proq ushlab turilganda Unicode-ning vakolatiga ega
bo'lgan tugmachalarning aksariyati ushbu tartibda qayta-qayta KEY_PRESSED va
KEY_TYPED hodisalarni keltirib chiqaradi . Ushbu xatti-harakatni aks ettiradigan
kalitlar to'plami va ularning bajarilish tezligi boshqarilmaydi va platformaga xosdir.
Har bir KeyEvent SHIFT, CTRL, ALT va META tugmachalari holatini
belgilaydigan modifikatorlarni to'plamiga ega . Bu InputEvent bitinal yoki OR ning
natijasi bo'lgan int qiymati. Tadbir vaqtida qaysi tugmalarni bosganiga qarab
SHIFT_MASK,
InputEvent.CTRL_MASK,
InputEvent.ALT_MASK
va
InputEvent.META_ MASK . Ushbu qiymatni getModifiers () yordamida olishimiz
mumkin va voqea isShiftDown (), isControlDown (), isAltDown () va isMetaDown
() yordamida ushbu tugmachalarning qaysi biri bosilganligini aniq so'rashimiz
mumkin .
KeyEvent shuningdek boolean actionKey xususiyatini saqlab qoladi, bu esa
chaqirilayotgan klaviatura tugmachasi ushbu ilova tomonidan bajarilishi kerak
bo'lgan amalga (haqiqiy) va matn tarkibiy qismining hujjat tarkibiga qo'shimcha
(noto'g'ri) kabi ma'lumotlarga nisbatan mos kelishini belgilaydi. Ushbu
xususiyatning qiymatini olish uchun KeyEvent's isActionKey () usulidan
Dasturlash II fanidan 9-ma’ruza
9
foydalanishimiz mumkin .
private void jTextField2KeyPressed(java.awt.event.KeyEvent evt) {
int keyCode = evt.getKeyCode();
if (keyCode == 66) {
jLabel1.setText("Siz b harfini bostingiz");
displayInfo(evt, "KeyPressed");
}
}
private void jTextField2KeyReleased(java.awt.event.KeyEvent evt) {
int keyCode = evt.getKeyCode();
if (keyCode == 67) {
jLabel1.setText("Siz c harfini bostingiz");
displayInfo(evt, "KeyReleased");
}
}
private void jTextField2KeyTyped(java.awt.event.KeyEvent evt) {
displayInfo(evt, "KeyTyped");
}
protected void displayInfo(KeyEvent evt, String s){
String keystring, modstring, tmpstring, actionstring,locationstring;
int id=evt.getID();
if (id == KeyEvent.KEY_TYPED) {
char c = evt.getKeyChar();
keystring = "key character = '" + c + "'";
} else {
int keyCode = evt.getKeyCode();
keystring
=
"key
code
=
"
+
keyCode
+
"
("
+
KeyEvent.getKeyText(keyCode) + ")";
}
int modifiers = evt.getModifiersEx();
modstring = "modifiers = " + modifiers;
tmpstring = KeyEvent.getModifiersExText(modifiers);
if (tmpstring.length() > 0) {
modstring += " (" + tmpstring + ")";
} else {
modstring += " (no modifiers)";
}
Dasturlash II fanidan 9-ma’ruza
10
actionstring = "action key? ";
if (evt.isActionKey()) {
actionstring += "YES";
} else {
actionstring += "NO";
}
locationstring = "key location: ";
int location = evt.getKeyLocation();
if (location == KeyEvent.KEY_LOCATION_STANDARD) {
locationstring += "standard";
} else if (location == KeyEvent.KEY_LOCATION_LEFT) {
locationstring += "left";
} else if (location == KeyEvent.KEY_LOCATION_RIGHT) {
locationstring += "right";
} else if (location == KeyEvent.KEY_LOCATION_NUMPAD) {
locationstring += "numpad";
} else { // (location == KeyEvent.KEY_LOCATION_UNKNOWN)
locationstring += "unknown";
}
jTextArea1.
append
(keystring+"\n"+modstring+"\n"+actionstring+"\n"+locationstri
ng);
}
Do'stlaringiz bilan baham: |