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