Looking at current mouse button state

This is a discussion on Looking at current mouse button state within the C# Programming forums, part of the General Programming Boards category; I have a small portion of code that I wish to bypass if the left mouse button is down. It ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    27

    Looking at current mouse button state

    I have a small portion of code that I wish to bypass if the left mouse button is down. It seems a waste to use a boolean that gets set by mouse events for a single check. However, I cannot seem to find any information on how to go about just peeking at the state of the mouse buttons. Is it not possible, or is it just so simple that its never been an issue?

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    I don't think you can peek at the state of the mouse buttons. I think the mouse only transmits information when the mouse's state has changed and is incapable of responding to external queries. The events sounds like your best bet.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    27
    Unfortunate, but thank you anyway.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    27
    I just found a way to do it. For others searching for this in the future, here it is...

    If you are using System.Windows.Forms, there is a Control class which contains a Static MouseButtons property. This property gets the bitwise combination representing the mouse button states.

    if((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left) for example will check if the left mouse button is down.

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Nice...good info. You might want to consider what logical operator to use. For instance, ((Control.MouseButtons & MouseButtons.Left) != 0) returns true if the left mouse button is down, but others may be also. If you want to check to see if only the left mouse button is down, you could do ((Control.MouseButtons ^ MouseButtons.Left) == 0).

    However, your way works too.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. Mouse Events
    By disruptor108 in forum C# Programming
    Replies: 0
    Last Post: 01-23-2007, 10:40 PM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 09:16 AM
  5. Mouse button outside of my app?
    By BubbleMan in forum Windows Programming
    Replies: 1
    Last Post: 10-01-2001, 03:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21