1 7 package java.awt; 8 9 import java.awt.event.*; 10 import java.io.*; 11 12 45 public class Event implements java.io.Serializable { 46 private transient long data; 47 48 49 50 54 public static final int SHIFT_MASK = 1 << 0; 55 56 60 public static final int CTRL_MASK = 1 << 1; 61 62 67 public static final int META_MASK = 1 << 2; 68 69 74 public static final int ALT_MASK = 1 << 3; 75 76 77 78 81 public static final int HOME = 1000; 82 83 86 public static final int END = 1001; 87 88 91 public static final int PGUP = 1002; 92 93 96 public static final int PGDN = 1003; 97 98 101 public static final int UP = 1004; 102 103 106 public static final int DOWN = 1005; 107 108 111 public static final int LEFT = 1006; 112 113 116 public static final int RIGHT = 1007; 117 118 121 public static final int F1 = 1008; 122 123 126 public static final int F2 = 1009; 127 128 131 public static final int F3 = 1010; 132 133 136 public static final int F4 = 1011; 137 138 141 public static final int F5 = 1012; 142 143 146 public static final int F6 = 1013; 147 148 151 public static final int F7 = 1014; 152 153 156 public static final int F8 = 1015; 157 158 161 public static final int F9 = 1016; 162 163 166 public static final int F10 = 1017; 167 168 171 public static final int F11 = 1018; 172 173 176 public static final int F12 = 1019; 177 178 181 public static final int PRINT_SCREEN = 1020; 182 183 186 public static final int SCROLL_LOCK = 1021; 187 188 191 public static final int CAPS_LOCK = 1022; 192 193 196 public static final int NUM_LOCK = 1023; 197 198 201 public static final int PAUSE = 1024; 202 203 206 public static final int INSERT = 1025; 207 208 209 210 213 public static final int ENTER = '\n'; 214 215 218 public static final int BACK_SPACE = '\b'; 219 220 223 public static final int TAB = '\t'; 224 225 228 public static final int ESCAPE = 27; 229 230 233 public static final int DELETE = 127; 234 235 236 237 private static final int WINDOW_EVENT = 200; 238 239 242 public static final int WINDOW_DESTROY = 1 + WINDOW_EVENT; 243 244 247 public static final int WINDOW_EXPOSE = 2 + WINDOW_EVENT; 248 249 252 public static final int WINDOW_ICONIFY = 3 + WINDOW_EVENT; 253 254 257 public static final int WINDOW_DEICONIFY = 4 + WINDOW_EVENT; 258 259 262 public static final int WINDOW_MOVED = 5 + WINDOW_EVENT; 263 264 265 private static final int KEY_EVENT = 400; 266 267 270 public static final int KEY_PRESS = 1 + KEY_EVENT; 271 272 275 public static final int KEY_RELEASE = 2 + KEY_EVENT; 276 277 285 public static final int KEY_ACTION = 3 + KEY_EVENT; 286 287 295 public static final int KEY_ACTION_RELEASE = 4 + KEY_EVENT; 296 297 298 private static final int MOUSE_EVENT = 500; 299 300 308 public static final int MOUSE_DOWN = 1 + MOUSE_EVENT; 309 310 318 public static final int MOUSE_UP = 2 + MOUSE_EVENT; 319 320 323 public static final int MOUSE_MOVE = 3 + MOUSE_EVENT; 324 325 328 public static final int MOUSE_ENTER = 4 + MOUSE_EVENT; 329 330 333 public static final int MOUSE_EXIT = 5 + MOUSE_EVENT; 334 335 343 public static final int MOUSE_DRAG = 6 + MOUSE_EVENT; 344 345 346 347 private static final int SCROLL_EVENT = 600; 348 349 353 public static final int SCROLL_LINE_UP = 1 + SCROLL_EVENT; 354 355 359 public static final int SCROLL_LINE_DOWN = 2 + SCROLL_EVENT; 360 361 365 public static final int SCROLL_PAGE_UP = 3 + SCROLL_EVENT; 366 367 371 public static final int SCROLL_PAGE_DOWN = 4 + SCROLL_EVENT; 372 373 378 public static final int SCROLL_ABSOLUTE = 5 + SCROLL_EVENT; 379 380 383 public static final int SCROLL_BEGIN = 6 + SCROLL_EVENT; 384 385 388 public static final int SCROLL_END = 7 + SCROLL_EVENT; 389 390 391 private static final int LIST_EVENT = 700; 392 393 396 public static final int LIST_SELECT = 1 + LIST_EVENT; 397 398 401 public static final int LIST_DESELECT = 2 + LIST_EVENT; 402 403 404 private static final int MISC_EVENT = 1000; 405 406 409 public static final int ACTION_EVENT = 1 + MISC_EVENT; 410 411 414 public static final int LOAD_FILE = 2 + MISC_EVENT; 415 416 419 public static final int SAVE_FILE = 3 + MISC_EVENT; 420 421 424 public static final int GOT_FOCUS = 4 + MISC_EVENT; 425 426 429 public static final int LOST_FOCUS = 5 + MISC_EVENT; 430 431 439 public Object target; 440 441 448 public long when; 449 450 458 public int id; 459 460 467 public int x; 468 469 476 public int y; 477 478 485 public int key; 486 487 490 492 501 public int modifiers; 502 503 512 public int clickCount; 513 514 521 public Object arg; 522 523 531 public Event evt; 532 533 534 private static final int actionKeyCodes[][] = { 535 536 { KeyEvent.VK_HOME, Event.HOME }, 537 { KeyEvent.VK_END, Event.END }, 538 { KeyEvent.VK_PAGE_UP, Event.PGUP }, 539 { KeyEvent.VK_PAGE_DOWN, Event.PGDN }, 540 { KeyEvent.VK_UP, Event.UP }, 541 { KeyEvent.VK_DOWN, Event.DOWN }, 542 { KeyEvent.VK_LEFT, Event.LEFT }, 543 { KeyEvent.VK_RIGHT, Event.RIGHT }, 544 { KeyEvent.VK_F1, Event.F1 }, 545 { KeyEvent.VK_F2, Event.F2 }, 546 { KeyEvent.VK_F3, Event.F3 }, 547 { KeyEvent.VK_F4, Event.F4 }, 548 { KeyEvent.VK_F5, Event.F5 }, 549 { KeyEvent.VK_F6, Event.F6 }, 550 { KeyEvent.VK_F7, Event.F7 }, 551 { KeyEvent.VK_F8, Event.F8 }, 552 { KeyEvent.VK_F9, Event.F9 }, 553 { KeyEvent.VK_F10, Event.F10 }, 554 { KeyEvent.VK_F11, Event.F11 }, 555 { KeyEvent.VK_F12, Event.F12 }, 556 { KeyEvent.VK_PRINTSCREEN, Event.PRINT_SCREEN }, 557 { KeyEvent.VK_SCROLL_LOCK, Event.SCROLL_LOCK }, 558 { KeyEvent.VK_CAPS_LOCK, Event.CAPS_LOCK }, 559 { KeyEvent.VK_NUM_LOCK, Event.NUM_LOCK }, 560 { KeyEvent.VK_PAUSE, Event.PAUSE }, 561 { KeyEvent.VK_INSERT, Event.INSERT } 562 }; 563 564 572 private boolean consumed = false; 573 574 577 private static final long serialVersionUID = 5488922509400504703L; 578 579 static { 580 581 Toolkit.loadLibraries(); 582 if (!GraphicsEnvironment.isHeadless()) { 583 initIDs(); 584 } 585 } 586 587 591 private static native void initIDs(); 592 593 611 public Event(Object target, long when, int id, int x, int y, int key, 612 int modifiers, Object arg) { 613 this.target = target; 614 this.when = when; 615 this.id = id; 616 this.x = x; 617 this.y = y; 618 this.key = key; 619 this.modifiers = modifiers; 620 this.arg = arg; 621 this.data = 0; 622 this.clickCount = 0; 623 switch(id) { 624 case ACTION_EVENT: 625 case WINDOW_DESTROY: 626 case WINDOW_ICONIFY: 627 case WINDOW_DEICONIFY: 628 case WINDOW_MOVED: 629 case SCROLL_LINE_UP: 630 case SCROLL_LINE_DOWN: 631 case SCROLL_PAGE_UP: 632 case SCROLL_PAGE_DOWN: 633 case SCROLL_ABSOLUTE: 634 case SCROLL_BEGIN: 635 case SCROLL_END: 636 case LIST_SELECT: 637 case LIST_DESELECT: 638 consumed = true; break; 640 default: 641 } 642 } 643 644 661 public Event(Object target, long when, int id, int x, int y, int key, int modifiers) { 662 this(target, when, id, x, y, key, modifiers, null); 663 } 664 665 676 public Event(Object target, int id, Object arg) { 677 this(target, 0, id, 0, 0, 0, 0, arg); 678 } 679 680 696 public void translate(int dx, int dy) { 697 this.x += dx; 698 this.y += dy; 699 } 700 701 713 public boolean shiftDown() { 714 return (modifiers & SHIFT_MASK) != 0; 715 } 716 717 729 public boolean controlDown() { 730 return (modifiers & CTRL_MASK) != 0; 731 } 732 733 746 public boolean metaDown() { 747 return (modifiers & META_MASK) != 0; 748 } 749 750 755 void consume() { 756 switch(id) { 757 case KEY_PRESS: 758 case KEY_RELEASE: 759 case KEY_ACTION: 760 case KEY_ACTION_RELEASE: 761 consumed = true; 762 break; 763 default: 764 } 766 } 767 768 773 boolean isConsumed() { 774 return consumed; 775 } 776 777 785 static int getOldEventKey(KeyEvent e) { 786 int keyCode = e.getKeyCode(); 787 for (int i = 0; i < actionKeyCodes.length; i++) { 788 if (actionKeyCodes[i][0] == keyCode) { 789 return actionKeyCodes[i][1]; 790 } 791 } 792 return (int)e.getKeyChar(); 793 } 794 795 803 char getKeyEventChar() { 804 for (int i = 0; i < actionKeyCodes.length; i++) { 805 if (actionKeyCodes[i][1] == key) { 806 return KeyEvent.CHAR_UNDEFINED; 807 } 808 } 809 return (char)key; 810 } 811 812 825 protected String paramString() { 826 String str = "id=" + id + ",x=" + x + ",y=" + y; 827 if (key != 0) { 828 str += ",key=" + key; 829 } 830 if (shiftDown()) { 831 str += ",shift"; 832 } 833 if (controlDown()) { 834 str += ",control"; 835 } 836 if (metaDown()) { 837 str += ",meta"; 838 } 839 if (target != null) { 840 str += ",target=" + target; 841 } 842 if (arg != null) { 843 str += ",arg=" + arg; 844 } 845 return str; 846 } 847 848 859 public String toString() { 860 return getClass().getName() + "[" + paramString() + "]"; 861 } 862 } 863 | Popular Tags |