We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,376 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Weird C# Error

Hello. I have a really weird problem. Whenever I try to get if the user pressed down on the mouse (clicked), via GetAsyncKeyState(), it only shows it some times. I tried the exact same code, but with another key Ex: A, and it works fine.

Here is the code.

public void GetMouse()
        {

            foreach (System.Int32 i in Enum.GetValues(typeof(Keys)))
            {
                XPos.Add((ushort)Cursor.Position.X);
                YPos.Add((ushort)Cursor.Position.Y);

                int pressed = GetAsyncKeyState(i);

                if ((pressed == -32767))
                {
                    if ((i == 1) || (i == 2))
                        Click.Add((ushort)i);
                    else
                        Click.Add(0);
                }
                else
                    Click.Add(0);
            }
        }

This goes through a loop, so it is always checking.
Oh, and also I've tried this on multiple windows, and such, and it still doesn't work.

Thank you so much, Glut.

4
Contributors
4
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
5
Views
glut
Light Poster
42 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Have you considered using the MouseClick property of the form instead?

MikeyIsMe
Master Poster
798 posts since Nov 2010
Reputation Points: 88
Solved Threads: 69
Skill Endorsements: 10

That seems like a very CPU expensive loop to poll for mouse clicks. It has to go through hundreds (maybe even thousands) of enums, poll the GetAsyncKeyState, filter the results, then set flags. An event would have a LOT less overhead.

As for why it's not working - I have no idea. Never used GetAsyncKeyState before. Maybe put this in context for us (what are you trying to accomplish) and we can help come up with a better solution.

skatamatic
Posting Shark
986 posts since Nov 2007
Reputation Points: 403
Solved Threads: 132
Skill Endorsements: 1

I agree with MikeyIsMe, use the MouseClick event.

But then again, not knowing what the OP wants..

phoenix911
Junior Poster
170 posts since May 2009
Reputation Points: 38
Solved Threads: 21
Skill Endorsements: 0

I agree with MikeyIsMe, use the MouseClick event.

But then again, not knowing what the OP wants..

Getting it globally. I'll try Thread.Sleep(100); and get back to you guys. Thanks :)

glut
Light Poster
42 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0703 seconds using 2.73MB