I am trying to sense multiple keys at a time and I am wondering how to use a hash map to do this.
jon.kiparsky 326 Posting Virtuoso
What do you mean? A hashmap maps key,value pairs - is that the sense of "key" you mean, or are you talking about "key" as in "keyboard" or do you mean "key" as in crypto, or what do you mean?
sirlink99 56 Practically a Master Poster
I mean keys as in keyboard keys
mKorbel 274 Veteran Poster
great topic, great sugestion, stupid reply
but you are big star on this small heaven(s) then here comings ...
http://download.oracle.com/javase/tutorial/uiswing/events/index.html
don't forget look at KeyBindings too with my intention to avoids to continue this thread :-)
jon.kiparsky 326 Posting Virtuoso
You don't use hash maps to sense keyboard keys, you use keyboard listeners for that.
Are you talking about handling a multiple key press scenario after it's detected? Like, user presses 'a', 'd', and 'e' at the same time?
I can imagine one way to do that using a hash map, but forget about the hash map for the moment. What is it you're trying to do? Figure out the requirements, then worry about how you'll implement it.
sirlink99 56 Practically a Master Poster
I am trying to sense 2 keys, CTRL and SPACE, but keylistener to my knowledge can only sense one key at a time unless both keys are pressed at the same time, in my cast they most likely wont be.
Edited by sirlink99 because: n/a
jon.kiparsky 326 Posting Virtuoso
You'll probably need to use keypressed and keyreleased.
When keypressed fires, if the firing key is "CTRL" set a flag, controlDown, to true
When keyreleased fires, if the firing key is "CTRL", set that flag to false
If keypressed fires, and the firing key is "SPACE", check that flag to see what you need to do.
I'm sure there are other ways to handle this, some of which might be better. Probably reading through the tutorial that was posted would be a good idea.
sirlink99 56 Practically a Master Poster
here is my code. It doesn't seem to work and I don't know why.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class CubeTimer extends JFrame implements ActionListener, KeyListener{
double versionNum = 1.0; JFrame helpFrame;
JFrame frame;
JButton ok;
Timer timer = new Timer (1,this);
int time = 0;
double dispTime;
int going = 0;
JLabel display;
boolean ctrl = false;
boolean space = false;
boolean wasCtrl = false;
boolean wasSpace = false;
public CubeTimer (){
frame = new JFrame ("WP CubeTimer");
frame.setSize (400,300);
JPanel pane = new JPanel ();
JMenuBar menubar = new JMenuBar();
JMenu file = new JMenu ("File");
JMenu help = new JMenu ("Help");
JMenuItem close = new JMenuItem ("Close"); close.addActionListener(this); close.setActionCommand ("Close");
JMenuItem helpOption = new JMenuItem ("Help"); helpOption.addActionListener(this); helpOption.setActionCommand("help");
JMenuItem about = new JMenuItem ("About");about.addActionListener (this); about.setActionCommand ("About");
file.add(close);
help.add(helpOption);
help.add(about);
menubar.add(file);
menubar.add(help);
frame.setJMenuBar(menubar);
display = new JLabel ("Press control and space to start/stop the timer.");
pane.add (display);
frame.add(pane);
frame.setVisible(true);
}
public static void main (String[] args){
CubeTimer ct = new CubeTimer ();
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_SPACE){
space = true;
if (ctrl = true && going == 0){
timer.start();
going = 1;
}
if (ctrl = true && going == 1 && wasCtrl != false && wasSpace != false){
timer.stop();
}
}
if (key == KeyEvent.VK_CONTROL){
ctrl = true;
if (space = true && going == 0 && wasCtrl != false && wasSpace != false){
timer.start();
going = 1;
}
else if (space = true && going == 1){
timer.stop();
}
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
int key = e.getKeyCode();
if (key == KeyEvent.VK_SPACE){
space = false;
wasSpace = true;
}
if (key == KeyEvent.VK_CONTROL){
ctrl = false;
wasCtrl = true;
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if ("About".equals(e.getActionCommand ())){
JFrame aboutFrame = new JFrame ("About");
aboutFrame.setSize (400,300);
aboutFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel aboutPane = new JPanel ();
aboutPane.setLayout (new GridLayout (4,1));
ImageIcon logoImg = new ImageIcon ("LogoBlank.png");
JLabel logoLabel = new JLabel (logoImg);
aboutPane.add(logoLabel);
JLabel version = new JLabel ("Version: " + versionNum, JLabel.CENTER);
aboutPane.add (version);
JLabel madeBy = new JLabel ("Made By: Warrior Productions (Adam Balint)", JLabel.CENTER);
aboutPane.add(madeBy);
JLabel email = new JLabel ("<html>Contact: <a href=\"WarriorProd@hotmail.com\"> WarriorProd@hotmail.com </a> </html> ", JLabel.CENTER);
aboutPane.add (email);
aboutFrame.add(aboutPane);
aboutFrame.setVisible(true);
}
else if ("Close".equals(e.getActionCommand ())){
frame.dispose();
}
else if ("help".equals(e.getActionCommand())){
helpFrame = new JFrame ("Help");
helpFrame.setSize (550,150);
helpFrame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
JPanel pane = new JPanel ();
JLabel instruct = new JLabel ("<html> <center>To start the timer: hold down left control and space. When released then the timer starts. <br>" + "<br>" + "To Stop the timer: Press both the left control and spacebar to stop the timer. </center></html>");
ok = new JButton ("OK"); ok.addActionListener (this);ok.setActionCommand ("helpOK");
pane.add (instruct);
pane.add (ok);
helpFrame.add(pane);
helpFrame.setVisible(true);
}
else if ("helpOK".equals (e.getActionCommand())){
helpFrame.dispose();
}
else {
time += 1;
dispTime = time/100;
display.setText ("" +dispTime);
}
}
}
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
You have been on DaniWeb long enough to know that "It doesn't seem to work" isn't good enough.
If you want someone to help, please give a proper description of your problem and the things you have already done to try to solve it.
sirlink99 56 Practically a Master Poster
I am pretty sure that sensing the keys isn't working. I changed it so the timer starts on space. It didn't start. So it is something with the keys.
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
Have you put print statements into your code to confirm whether the right things are being called? Don't try to guess that kind of thing - test whether it's right.
Did you remember to add the KeyListener to the relevant components??
sirlink99 56 Practically a Master Poster
That works somewhat. Here is my new code. I fixed the keysListener and added print statements, but now it starts when I press space not when I press both space and ctrl at the same time.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class CubeTimer extends JFrame implements ActionListener, KeyListener{
double versionNum = 1.0; JFrame helpFrame;
JFrame frame;
JButton ok;
Timer timer = new Timer (1,this);
int time = 0;
double dispTime;
int going = 0;
JLabel display;
boolean ctrl = false;
boolean space = false;
boolean wasCtrl = false;
boolean wasSpace = false;
public CubeTimer (){
frame = new JFrame ("WP CubeTimer");
frame.setSize (400,300);
JPanel pane = new JPanel ();
JMenuBar menubar = new JMenuBar();
JMenu file = new JMenu ("File");
JMenu help = new JMenu ("Help");
JMenuItem close = new JMenuItem ("Close"); close.addActionListener(this); close.setActionCommand ("Close");
JMenuItem helpOption = new JMenuItem ("Help"); helpOption.addActionListener(this); helpOption.setActionCommand("help");
JMenuItem about = new JMenuItem ("About");about.addActionListener (this); about.setActionCommand ("About");
frame.addKeyListener(this);
file.add(close);
help.add(helpOption);
help.add(about);
menubar.add(file);
menubar.add(help);
frame.setJMenuBar(menubar);
display = new JLabel ("Press control and space to start/stop the timer.");
pane.add (display);
frame.add(pane);
frame.setVisible(true);
}
public static void main (String[] args){
CubeTimer ct = new CubeTimer ();
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_SPACE){
space = true;
System.out.println ("space");
if (ctrl = true && going == 0){
timer.start();
going = 1;
System.out.println ("space/ CTRL");
}
if (ctrl = true && going == 1 && wasCtrl != false && wasSpace != false){
timer.stop();
}
}
if (key == KeyEvent.VK_CONTROL){
ctrl = true;
System.out.println ("CTRL");
if (space = true && going == 0 && wasCtrl != false && wasSpace != false){
timer.start();
going = 1;
System.out.println ("space/ CTRL");
}
else if (space = true && going == 1){
timer.stop();
}
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
int key = e.getKeyCode();
if (key == KeyEvent.VK_SPACE){
space = false;
System.out.println ("Space false");
//wasSpace = true;
}
if (key == KeyEvent.VK_CONTROL){
ctrl = false;
System.out.println ("ctrl false");
//wasCtrl = true;
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if ("About".equals(e.getActionCommand ())){
JFrame aboutFrame = new JFrame ("About");
aboutFrame.setSize (400,300);
aboutFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel aboutPane = new JPanel ();
aboutPane.setLayout (new GridLayout (4,1));
ImageIcon logoImg = new ImageIcon ("LogoBlank.png");
JLabel logoLabel = new JLabel (logoImg);
aboutPane.add(logoLabel);
JLabel version = new JLabel ("Version: " + versionNum, JLabel.CENTER);
aboutPane.add (version);
JLabel madeBy = new JLabel ("Made By: Warrior Productions (Adam Balint)", JLabel.CENTER);
aboutPane.add(madeBy);
JLabel email = new JLabel ("<html>Contact: <a href=\"WarriorProd@hotmail.com\"> WarriorProd@hotmail.com </a> </html> ", JLabel.CENTER);
aboutPane.add (email);
aboutFrame.add(aboutPane);
aboutFrame.setVisible(true);
}
else if ("Close".equals(e.getActionCommand ())){
frame.dispose();
}
else if ("help".equals(e.getActionCommand())){
helpFrame = new JFrame ("Help");
helpFrame.setSize (550,150);
helpFrame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
JPanel pane = new JPanel ();
JLabel instruct = new JLabel ("<html> <center>To start the timer: hold down left control and space. When released then the timer starts. <br>" + "<br>" + "To Stop the timer: Press both the left control and spacebar to stop the timer. </center></html>");
ok = new JButton ("OK"); ok.addActionListener (this);ok.setActionCommand ("helpOK");
pane.add (instruct);
pane.add (ok);
helpFrame.add(pane);
helpFrame.setVisible(true);
}
else if ("helpOK".equals (e.getActionCommand())){
helpFrame.dispose();
}
else {
time += 1;
dispTime = time/100;
display.setText ("" +dispTime);
}
}
}
mKorbel 274 Veteran Poster
KeyBindings
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.