When I press ALT on a JFrame, and I press the down or up arrow key, a menu appears. How do I disable this so I can actually use ALT in game?
Thanks.
When I press ALT on a JFrame, and I press the down or up arrow key, a menu appears. How do I disable this so I can actually use ALT in game?
Thanks.
What exactly are you asking? (alt+tab==bad move!)
Ooops lol, I meant alt. You know when you press it, it shows a menu on a JFrame? I need that to be disabled so I can use alt for jump.
I’ve used alt keys in JFrames with no problem… Can you post a cutdown example demonstrating the problem?
I think I know what the problem is, copying and pasting code before getting to know what it does.
You added the Alt + key combinations to your JMenu without knowing it.
say this is a start button on your menu, having Alt+A being enabled to use it
start_button = new JMenuItem("Both text and icon",
new ImageIcon("images/start.gif"));
start_button.setMnemonic(KeyEvent.VK_A);
menu.add(start_button);
You shouldn’t have that this line of code because it enables Alt for JMenu use.
start_button.setMnemonic(KeyEvent.VK_A);
“A” being changeable
if it still pops up a menu on your game after taking out all of these, there might be one more thing into your menu.
post your code here like SimonH recommended.
I don’t have a JMenu. Its the default menu:
http://img181.imageshack.us/img181/2070/menull7.jpg
I’ve also tried setFocusTraversalKeysEnabled(false)
Hmmm… JFrame doesn’t have a default menu - you have to set one…
Can’t really help unless you post your code!
exactly why I was wondering if he used JFrame and JMenu…
JFrame is just a blank frame until you add menus and text and such
I’ve had this happen, but i don’t know where (still have).
Are you sure its not something laf related? because if it is and i can find the key its out of there.
Edit: To clarify, i don’t have a jmenu either, just a Jframe+custom Jcomponent + ocasional JDialog + ocasional JPopupMenu.
My code:
public void init() {
try {
processor = new PacketHandler();
} catch (Exception e) {
Console.w = false;
e.printStackTrace();
ErrorDialog ed = new ErrorDialog(Console.getWindow(), "Server unavailable", "Connecting to the server failed.");
ed.setLocation(getScreenCenter(ed));
ed.setTitle("Server Unavailable");
ed.getCloseButton().addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent ae) {
System.exit(0);
}
});
ed.setVisible(true);
Console.getWindow().dispose();
Console.getApplication().setWindow(null);
return;
}
initComponents();
setMinimumSize(new Dimension(800, 600));
setMaximumSize(new Dimension(800, 600));
setResizable(false);
ImageTransparencyTransformer t = ImageTransparencyTransformer.getInstance();
setIconImage(
t.filter(getToolkit().createImage(getClass().getResource("resources/images/icon.png")))
);
setLocation(getToolkit().getScreenSize().width / 2 - getSize().width / 2, getToolkit().getScreenSize().height / 2 - getSize().height / 2);
addWindowListener(new WindowAdapter() {
@Override public void windowClosing(WindowEvent we) {
close();
}
});
setTitle("Renoria");
try {
macro = new Robot();
} catch (Exception e) {
e.printStackTrace();
ScreenManager s = ScreenManager.getInstance();
s.restoreScreen();
ErrorDialog d = new ErrorDialog(this, "GUI Error", "Creating the User Interface failed.");
d.setVisible(true);
this.setVisible(false);
}
macro.mouseMove(getToolkit().getScreenSize().width / 2,
getToolkit().getScreenSize().height / 2);
Cursor c = getToolkit().createCustomCursor(getToolkit().createImage(""),
new Point(0,0), "");
setCursor(c);
setAlwaysOnTop(true);
cursor = getToolkit().createImage(getClass().getResource("resources/images/cursor.png"));
cursor = t.filter(cursor);
defaultCursor = cursor;
final AbstractMedia am = this.media;
final MainWindow wi = this;
addKeyListener(new KeyListener() {
@Override public void keyPressed(KeyEvent key) {
if (key.getKeyCode() == KeyEvent.VK_F12) {
takeScreenShot("Renoria-");
return;
}
if (key.getKeyCode() == KeyEvent.VK_ENTER) {
if (key.isAltDown()) {
key.consume();
ScreenManager s = ScreenManager.getInstance();
if (s.getFullScreenWindow() != null) {
s.restoreScreen();
} else {
s.setFullScreen(dis);
}
return;
}
}
if (media != null)
media.keyPressed(key);
}
public void keyTyped(KeyEvent key) {
if (media != null)
media.keyTyped(key);
}
public void keyReleased(KeyEvent key) {
if (media != null)
media.keyReleased(key);
if (key.getKeyCode() == KeyEvent.VK_ESCAPE && Console.testing) {
//System.exit(0);
}
}
});
setFocusTraversalKeysEnabled(false);
}
Just did an experiment. using even this code:
public static void main(String[] a) {
JFrame w = new JFrame("this sux");
w.setSize(300, 300);
w.setVisible(true);
}
creates that menu.
Must be some system settings on your end. Your ALT-click behaviour is equivalent to the typical right-click behaviour in the title bar (or iconized window on taskbar). Weird though, this popup menu does not have hotkeys on my system.
Nor mine! Strange…
What OS? Vista?
In XP pressing alt does indeed give focus to the title bar, altering the behaviour of subsequent key presses. (up & down cursors will cause the menu you describe to appear, while other keys will be consumed by the windowing system, and depending upon the key being pressed may return the focus back to the application)
Without native code I don’t think there is anything you can do about it.
Although… you do receive a key event for the initial ‘Alt’ key press. Perhaps if you issued a requestFocus event in response to this keypress, it would interrupt the windowing system’s title bar gaining focus.
Hey, Renoria, could you try Jetp4k (uses arrow keys, has alt as a fire key) and see if that screws up? If not I’ll get you the source code.
Yep, in XP i’m seeing the issue he’s described.
i am not sure if this will work but have you tried to consume all alt key events?
//end of key pressed method
if (key.isAltDown()) {
key.consume();
}
I get the same problem in Vista64 Home Premium.
Pressing alt+arrow at the same time does nothing but pressing alt by itself, then up or down by itself, brings up the annoying menu.
http://img522.imageshack.us/img522/1457/gaymenudr5.jpg
Yeah, just played JetP4k on vista with java webstart. Just got this annoying menu to popup. Also, pressing right or left then alt causes you to keep moving right/left without stop even if you’re not pressing any keys.
Anyway, would using a robot to press Alt again work?
Dammit! Mind you, I put the alt==fire thing in just for mac users…
Best I can suggest is that you don’t use the alt key in the way that you are - is there a sensible alternative?