Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Custom Cursor disabled by alt key  (Read 1495 times)
0 Members and 1 Guest are viewing this topic.
Offline zoto

Senior Devvie


Medals: 4



« Posted 2010-09-06 22:18:30 »

I noticed a strange problem that sometimes showed up in my game where the custom cursor would be disabled if the alt key is pressed.
I tracked the problem down to the custom cursor will be disabled when the alt key is pressed if and only if the mouse isn't moved and no other key is pressed in combination with the alt key.
Thinking this must be a bug in my game, I created a simple test program and was surprised to find the same problem.
I have searched google and JGO and not seen any mention of this problem.
I'm hoping someone knows a workaround or can point me to how I should be doing it different.

Here is the test program.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JFrame;


@SuppressWarnings("serial")
public class CustomCursorTest extends JFrame {
   BufferedImage image;
   Cursor cursor;
   
   public CustomCursorTest() {
      setPreferredSize(new Dimension(800, 600));
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      String url = "http://www.java-gaming.org/Themes/SMF_Revamped_v2c/images/on.gif";
      try {
         image = ImageIO.read( new URL(url) );
         cursor = Toolkit.getDefaultToolkit().createCustomCursor(image, new Point(), "Cursor");
         setCursor(cursor);
      } catch (Exception e) {
         e.printStackTrace();
      }
      pack();
   }
   
   @Override
   public void paint(Graphics g) {
      super.paint(g);
      g.drawImage(image, 100, 100, null);
   }

   /**
    * @param args
    */

   public static void main(String[] args) {
      CustomCursorTest test = new CustomCursorTest();
      test.setVisible(true);
   }


}

* edited for clarity.
Offline zoto

Senior Devvie


Medals: 4



« Reply #1 - Posted 2010-09-08 18:31:34 »

I found a workaround for now by simply consuming the key event. I can't help but think this could lead to strange side effects later.

1  
2  
3  
4  
5  
6  
   public void keyPressed(KeyEvent e) {
      if(e.isAltDown()){
         // handle alt events
         e.consume();
      }
   }
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #2 - Posted 2010-09-09 13:16:51 »

Using Frame.setUndecorated(true) might help.  Basically pressing alt with nothing else activates the menu system.  Even if you don't have a menu defined there is always the system menu attached to any decorated frame.  By removing decorations you remove the system menu.  But this also prevents the user from dragging/resizing the frame.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline princec

« JGO Spiffy Duke »


Medals: 429
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #3 - Posted 2010-09-09 13:42:27 »

Though you could then implement your own resizing and draggy widgets.

Cas Smiley

Offline zoto

Senior Devvie


Medals: 4



« Reply #4 - Posted 2010-09-09 15:35:04 »

Thanks for the replies, unfortunately I have the same problem with undecorated frames.
I didn't mention it before but a solution that would also work for applets would be ideal.

Following up on the menu's I found JMenuBar.setCursor(cursor) yet it didn't help either.
I also tried iterating over all the components returned by JFrame.getComponents() and calling setCursor.

Adding a key listener to consume the key is bothering me less every day.
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2010-09-09 20:27:24 »

getMenubar().setEnabled(false) ?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline zoto

Senior Devvie


Medals: 4



« Reply #6 - Posted 2010-09-10 17:13:22 »

The closest I found was getMenuBar() and I didn't see a method to disable it.
A JMenuBar returned with getJMenuBar() did have the setEnabled method but calling getJMenuBar().setEnabled(false); didn't seem to make a difference for this problem.

Both getMenu methods returned null by default and I simply set them with the default constructor for the class.
That may have something to do with it but i don't plan to use these menu systems so I didn't peruse it further.

Thanks for the replies, has anyone else run this and had the same problem? I haven't had a chance to test it on any other computers yet.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #7 - Posted 2010-09-10 18:39:23 »

In terms of side effects of consuming the event, I wouldn't worry about it unless you were hoping to have any alt-key defined actually working in your game.And sounds like you don't.

See my work:
OTC Software
Offline zoto

Senior Devvie


Medals: 4



« Reply #8 - Posted 2010-09-10 19:10:17 »

I do use the alt key but I just consume it after the fact so that fix hasn't caused any side effects.
It just felt hackish and made me think it's not "the right way", it doesn't really bother me now.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (114 views)
2014-11-26 15:22:04

toopeicgaming1999 (102 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!