Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
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  
  Silly key input question  (Read 1607 times)
0 Members and 1 Guest are viewing this topic.
Offline dranonymous

Junior Member




Hoping to become a Java Titan someday!


« Posted 2004-08-24 01:20:12 »

I need to check if the Shift key is pressed when moving the mouse.  It doesn't matter when is was pressed, just that is actually pressed at the moment I check.

I poked around a bit and can only find the info I already knew about keyListeners.  Then I came across key Bindings, which I really don't think is what I want either.

The reason behind my question is I am drawing a line.  If the user has the shift key down while moving the mouse, I want the line to be constrained either horizontally or vertically.  I can work that code out, but I need the magic -

isShiftPressed() method.  Smiley

Anyone have my magic method?

Regards,
Dr. A>
Offline kevglass

JGO Kernel


Medals: 159
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2004-08-24 04:56:46 »

java.awt.event.InputEvent:

boolean isShiftDown()
         Returns whether or not the Shift modifier is down on this event.

Since mouse event inherits indirectly from InputEvent you should be able to call it.

Whether its honoured on all systems!? Well, this is java, so write it once then test everywhere Smiley

Kev

Offline dranonymous

Junior Member




Hoping to become a Java Titan someday!


« Reply #2 - Posted 2004-08-24 12:22:05 »

Wee haw!  Test results will occur after I get back to the code tonight!

Thanks Kev!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline woogley
« Reply #3 - Posted 2004-08-24 12:45:05 »

I probably read the posts wrong, but just in case it isnt "honoured by all systems" you could just use a boolean Tongue

i.e:
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  
public class Example extends Canvas implements MouseMotionListener, KeyListener {
      boolean shiftDown;
      public Example() {
            shiftDown = false;
      }

      ...
      
      public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
                  shiftDown = true;
            }
      }
      public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_SHIFT) {
                  shiftDown = false;
            }
      }

      ...
      
      public void mouseMoved(MouseEvent e) {
            if (shiftDown) {
                  // constrain the line
           }
      }
}


edit: btw congrats on your new daughter Cheesy
Offline kevglass

JGO Kernel


Medals: 159
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #4 - Posted 2004-08-24 13:02:07 »

Would that actually work? I mean if you only press shift do you actually get a keyboard event? Since its a modifier I'd assumed you didn't get an event for shift.

This really is a question, I really don't know Smiley

Kev

EDIT: Lower case k on Kev! Whatever next!

Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #5 - Posted 2004-08-24 13:04:21 »

Yup, Shift works as a key event.  I use it as a key in my game to open up a certain menu and I haven't had problems on any system with it.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline kevglass

JGO Kernel


Medals: 159
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #6 - Posted 2004-08-24 13:12:31 »

Ah, in that case I'd use that rather than the mouse event modifier since its more likely to work Smiley

Kev

Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #7 - Posted 2004-08-24 13:18:03 »

All keys generate proper up/down events.

The exceptions are:
-extended keys (win/menu) and also print (iirc)
-tab(*)
and <alt gr> is alt+ctrl (duh)

(*=
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
DocComponentListener DCL=new DocComponentListener();
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor(DCL);
<whatever>.addKeyListener(DCL);
[...]
class DocComponentListener implements KeyListener, KeyEventPostProcessor
{
      public boolean postProcessKeyEvent(KeyEvent e)
      {
            //this one catches tab
           return true;
      }
      public void keyTyped(KeyEvent event)
      {
<etc>
}
)
The additional silly thing with tab is that the msvm generates usual keyevents for that thing, too.

http://people.freenet.de/ki_onyx/scancode.htm

The jar is a hybrid... it also spits some debug info out Smiley

弾幕 ☆ @mahonnaiseblog
Offline dranonymous

Junior Member




Hoping to become a Java Titan someday!


« Reply #8 - Posted 2004-08-24 16:00:53 »

You've all touched on some of the stuff I wanted to avoid with key input.  I understand that on some JVMs if  a key is held, you will get repeats of keyPressed or keypressed/keyreleased.  I really don't want the line visually toggling each time, hence the check for Shift at exactly the point they move the mouse.

I was also worried about the Tab/Shift/Alt, etc being special keys.  As long as I can check if Shift is pressed in the mouseEvent, then I'm probably good to go.

Regards,
Dr. A>

PS - woogley - Thanks for the congrats!  I sure love being a dad!

PPS - kEV - I'll try and get the name right next time.  Wink
Offline woogley
« Reply #9 - Posted 2004-08-24 16:46:46 »

Quote

I understand that on some JVMs if  a key is held, you will get repeats of keyPressed or keypressed/keyreleased.  I really don't want the line visually toggling each time, hence the check for Shift at exactly the point they move the mouse.


well, yes, that's the point of the boolean. as long as the shift key is held, the keyPressed event will be fired about every 10ms (this depends on the oprating system and settings though). since you're using a boolean to check if shift is down, you can check it any time you want regardless of any events going on. You wont notice the constant keyPress events because all thats happening is:

shiftDown = true;

every 10ms, which isnt exactly a bottleneck to worry about.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline dranonymous

Junior Member




Hoping to become a Java Titan someday!


« Reply #10 - Posted 2004-08-25 03:08:28 »

Tested out the info you all provided and I have now my drawing doing what I wanted.  Smiley

Thanks to all for the infos,
Dr. A>
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.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (40 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!