How to access spacebar while holding up arrow and left arrow
I am creating an Asteroids game and I need the up arrow, left arrow and spacebar to be detected when pressed. However, when I'm currently holding the arrow keys and press the spacebar, nothing happens and a little blue loading sign appears at my mouse-pointer. This leads me to believe that this is some sort of hotkey, because it works fine with the right arrow key. I'd appreciate an answer to guide me to the issue. I'm using java.awt.event.KeyListener to detect the keys. Thanks for your help!
I've tested this with the combination up arrow + right arrow + spacebar and it functions as it should. I googled the problem to no prevail. I just get problems people are having on Ubuntu, but I'm using Windows on a Lenovo Thinkpad.
@Override
public void keyPressed(KeyEvent e) {
keys.add(e.getKeyCode());
}
@Override
public void keyReleased(KeyEvent e) {
keys.remove(e.getKeyCode());
}
@Override
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == ' ' && beams.size() < 5) {
beams.add(new Beam((int)p.getPos().getX(), (int)p.getPos().getY(), p.getAngle()));
}
}
I expect to see the keyTyped function called even after all the other keys are pressed. Instead I receive a loading sign followed by a faint and low 'bing' sound.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Example extends JPanel implements KeyListener {
/**
*
*/
private static final long serialVersionUID = 1L;
//--------------------------------------------------
private boolean uparrow, leftarrow;
public Example () {
addKeyListener(this);
setFocusable(true);
}
public static void main (String args) {
JFrame f = new JFrame();
f.setSize(600, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Example());
f.setVisible(true);
}
public void paintComponent(Graphics g) {
if (uparrow && leftarrow) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, 600, 600);
}
}
@Override
public void keyPressed(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_UP) {
uparrow = true;
System.out.println("uparrow down!");
}
if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
leftarrow = true;
System.out.println("leftarrow down!");
}
}
@Override
public void keyReleased(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_UP) {
uparrow = false;
}
if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
leftarrow = false
}
}
@Override
public void keyTyped(KeyEvent arg0) {
if (arg0.getKeyChar() == ' ') {
System.out.println("space down!");
if (uparrow && leftarrow) {
repaint();
}
}
}
}
This example code.(I'm sorry for the bad indenting, I couldn't get it to work right) If you press the up-, and left-arrow keys, then follow up with the spacebar, nothing will happen. When you replace the 'VK_LEFT's with 'VK_RIGHT's the program works normally and a black rectangle appears in the JFrame.
java
New contributor
add a comment |
I am creating an Asteroids game and I need the up arrow, left arrow and spacebar to be detected when pressed. However, when I'm currently holding the arrow keys and press the spacebar, nothing happens and a little blue loading sign appears at my mouse-pointer. This leads me to believe that this is some sort of hotkey, because it works fine with the right arrow key. I'd appreciate an answer to guide me to the issue. I'm using java.awt.event.KeyListener to detect the keys. Thanks for your help!
I've tested this with the combination up arrow + right arrow + spacebar and it functions as it should. I googled the problem to no prevail. I just get problems people are having on Ubuntu, but I'm using Windows on a Lenovo Thinkpad.
@Override
public void keyPressed(KeyEvent e) {
keys.add(e.getKeyCode());
}
@Override
public void keyReleased(KeyEvent e) {
keys.remove(e.getKeyCode());
}
@Override
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == ' ' && beams.size() < 5) {
beams.add(new Beam((int)p.getPos().getX(), (int)p.getPos().getY(), p.getAngle()));
}
}
I expect to see the keyTyped function called even after all the other keys are pressed. Instead I receive a loading sign followed by a faint and low 'bing' sound.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Example extends JPanel implements KeyListener {
/**
*
*/
private static final long serialVersionUID = 1L;
//--------------------------------------------------
private boolean uparrow, leftarrow;
public Example () {
addKeyListener(this);
setFocusable(true);
}
public static void main (String args) {
JFrame f = new JFrame();
f.setSize(600, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Example());
f.setVisible(true);
}
public void paintComponent(Graphics g) {
if (uparrow && leftarrow) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, 600, 600);
}
}
@Override
public void keyPressed(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_UP) {
uparrow = true;
System.out.println("uparrow down!");
}
if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
leftarrow = true;
System.out.println("leftarrow down!");
}
}
@Override
public void keyReleased(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_UP) {
uparrow = false;
}
if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
leftarrow = false
}
}
@Override
public void keyTyped(KeyEvent arg0) {
if (arg0.getKeyChar() == ' ') {
System.out.println("space down!");
if (uparrow && leftarrow) {
repaint();
}
}
}
}
This example code.(I'm sorry for the bad indenting, I couldn't get it to work right) If you press the up-, and left-arrow keys, then follow up with the spacebar, nothing will happen. When you replace the 'VK_LEFT's with 'VK_RIGHT's the program works normally and a black rectangle appears in the JFrame.
java
New contributor
Please provide a Minimal, Complete, and Verifiable example. We should be able to copy/paste your code to our IDEs and have it run just like that. To help you we also need to know what libraries you're using.
– LuminousNutria
Dec 26 at 17:21
add a comment |
I am creating an Asteroids game and I need the up arrow, left arrow and spacebar to be detected when pressed. However, when I'm currently holding the arrow keys and press the spacebar, nothing happens and a little blue loading sign appears at my mouse-pointer. This leads me to believe that this is some sort of hotkey, because it works fine with the right arrow key. I'd appreciate an answer to guide me to the issue. I'm using java.awt.event.KeyListener to detect the keys. Thanks for your help!
I've tested this with the combination up arrow + right arrow + spacebar and it functions as it should. I googled the problem to no prevail. I just get problems people are having on Ubuntu, but I'm using Windows on a Lenovo Thinkpad.
@Override
public void keyPressed(KeyEvent e) {
keys.add(e.getKeyCode());
}
@Override
public void keyReleased(KeyEvent e) {
keys.remove(e.getKeyCode());
}
@Override
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == ' ' && beams.size() < 5) {
beams.add(new Beam((int)p.getPos().getX(), (int)p.getPos().getY(), p.getAngle()));
}
}
I expect to see the keyTyped function called even after all the other keys are pressed. Instead I receive a loading sign followed by a faint and low 'bing' sound.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Example extends JPanel implements KeyListener {
/**
*
*/
private static final long serialVersionUID = 1L;
//--------------------------------------------------
private boolean uparrow, leftarrow;
public Example () {
addKeyListener(this);
setFocusable(true);
}
public static void main (String args) {
JFrame f = new JFrame();
f.setSize(600, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Example());
f.setVisible(true);
}
public void paintComponent(Graphics g) {
if (uparrow && leftarrow) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, 600, 600);
}
}
@Override
public void keyPressed(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_UP) {
uparrow = true;
System.out.println("uparrow down!");
}
if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
leftarrow = true;
System.out.println("leftarrow down!");
}
}
@Override
public void keyReleased(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_UP) {
uparrow = false;
}
if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
leftarrow = false
}
}
@Override
public void keyTyped(KeyEvent arg0) {
if (arg0.getKeyChar() == ' ') {
System.out.println("space down!");
if (uparrow && leftarrow) {
repaint();
}
}
}
}
This example code.(I'm sorry for the bad indenting, I couldn't get it to work right) If you press the up-, and left-arrow keys, then follow up with the spacebar, nothing will happen. When you replace the 'VK_LEFT's with 'VK_RIGHT's the program works normally and a black rectangle appears in the JFrame.
java
New contributor
I am creating an Asteroids game and I need the up arrow, left arrow and spacebar to be detected when pressed. However, when I'm currently holding the arrow keys and press the spacebar, nothing happens and a little blue loading sign appears at my mouse-pointer. This leads me to believe that this is some sort of hotkey, because it works fine with the right arrow key. I'd appreciate an answer to guide me to the issue. I'm using java.awt.event.KeyListener to detect the keys. Thanks for your help!
I've tested this with the combination up arrow + right arrow + spacebar and it functions as it should. I googled the problem to no prevail. I just get problems people are having on Ubuntu, but I'm using Windows on a Lenovo Thinkpad.
@Override
public void keyPressed(KeyEvent e) {
keys.add(e.getKeyCode());
}
@Override
public void keyReleased(KeyEvent e) {
keys.remove(e.getKeyCode());
}
@Override
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == ' ' && beams.size() < 5) {
beams.add(new Beam((int)p.getPos().getX(), (int)p.getPos().getY(), p.getAngle()));
}
}
I expect to see the keyTyped function called even after all the other keys are pressed. Instead I receive a loading sign followed by a faint and low 'bing' sound.
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Example extends JPanel implements KeyListener {
/**
*
*/
private static final long serialVersionUID = 1L;
//--------------------------------------------------
private boolean uparrow, leftarrow;
public Example () {
addKeyListener(this);
setFocusable(true);
}
public static void main (String args) {
JFrame f = new JFrame();
f.setSize(600, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new Example());
f.setVisible(true);
}
public void paintComponent(Graphics g) {
if (uparrow && leftarrow) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, 600, 600);
}
}
@Override
public void keyPressed(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_UP) {
uparrow = true;
System.out.println("uparrow down!");
}
if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
leftarrow = true;
System.out.println("leftarrow down!");
}
}
@Override
public void keyReleased(KeyEvent arg0) {
if (arg0.getKeyCode() == KeyEvent.VK_UP) {
uparrow = false;
}
if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
leftarrow = false
}
}
@Override
public void keyTyped(KeyEvent arg0) {
if (arg0.getKeyChar() == ' ') {
System.out.println("space down!");
if (uparrow && leftarrow) {
repaint();
}
}
}
}
This example code.(I'm sorry for the bad indenting, I couldn't get it to work right) If you press the up-, and left-arrow keys, then follow up with the spacebar, nothing will happen. When you replace the 'VK_LEFT's with 'VK_RIGHT's the program works normally and a black rectangle appears in the JFrame.
java
java
New contributor
New contributor
edited 2 days ago
New contributor
asked Dec 26 at 15:31
J. Lengel
143
143
New contributor
New contributor
Please provide a Minimal, Complete, and Verifiable example. We should be able to copy/paste your code to our IDEs and have it run just like that. To help you we also need to know what libraries you're using.
– LuminousNutria
Dec 26 at 17:21
add a comment |
Please provide a Minimal, Complete, and Verifiable example. We should be able to copy/paste your code to our IDEs and have it run just like that. To help you we also need to know what libraries you're using.
– LuminousNutria
Dec 26 at 17:21
Please provide a Minimal, Complete, and Verifiable example. We should be able to copy/paste your code to our IDEs and have it run just like that. To help you we also need to know what libraries you're using.
– LuminousNutria
Dec 26 at 17:21
Please provide a Minimal, Complete, and Verifiable example. We should be able to copy/paste your code to our IDEs and have it run just like that. To help you we also need to know what libraries you're using.
– LuminousNutria
Dec 26 at 17:21
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
J. Lengel is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53934004%2fhow-to-access-spacebar-while-holding-up-arrow-and-left-arrow%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
J. Lengel is a new contributor. Be nice, and check out our Code of Conduct.
J. Lengel is a new contributor. Be nice, and check out our Code of Conduct.
J. Lengel is a new contributor. Be nice, and check out our Code of Conduct.
J. Lengel is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53934004%2fhow-to-access-spacebar-while-holding-up-arrow-and-left-arrow%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please provide a Minimal, Complete, and Verifiable example. We should be able to copy/paste your code to our IDEs and have it run just like that. To help you we also need to know what libraries you're using.
– LuminousNutria
Dec 26 at 17:21