]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/participant/KeyUtil.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / participant / KeyUtil.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 /*\r
13  *\r
14  * @author Toni Kalajainen\r
15  */\r
16 package org.simantics.g2d.participant;\r
17 \r
18 import java.util.HashSet;\r
19 import java.util.Set;\r
20 \r
21 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;\r
22 import org.simantics.scenegraph.g2d.events.KeyEvent;\r
23 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
24 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent;\r
25 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyReleasedEvent;\r
26 \r
27 \r
28 /**\r
29  * KeyStatus monitors statuses of keys.\r
30  * @see java.awt.event.KeyEvent\r
31  */\r
32 public class KeyUtil extends AbstractCanvasParticipant {        \r
33         \r
34         /** Key info */\r
35     private Set<Integer> pressedByKeyCode = new HashSet<Integer>();\r
36     private Set<Character> pressedByChar = new HashSet<Character>();\r
37         \r
38     @EventHandler(priority = Integer.MAX_VALUE)\r
39     public boolean handleClick(KeyEvent e) {\r
40         if (e instanceof KeyPressedEvent) {\r
41             pressedByKeyCode.add(e.keyCode);\r
42             pressedByChar.add(e.character);\r
43         } else\r
44         if (e instanceof KeyReleasedEvent) {\r
45             pressedByKeyCode.remove(e.keyCode);\r
46             pressedByChar.remove(e.character);\r
47         }\r
48         return false;\r
49     }\r
50     \r
51     public boolean isCharPressed(char c)\r
52     {\r
53         return pressedByChar.contains(c);\r
54     }\r
55     \r
56     /**\r
57      * See awt key event \r
58      * @see java.awt.event.KeyEvent\r
59      * @param keyCode\r
60      * @return\r
61      */\r
62     public boolean isKeyPressed(int keyCode)\r
63     {\r
64         return pressedByKeyCode.contains(keyCode);\r
65     }\r
66 \r
67 }\r