VB.NET PINVOKE loop GetKeyState

1

I have some error in the next loop and I do not know what it is since it does not return any errors, it just hangs up ..

While GetKeyState(Keys.Insert).ToString <> "-127" Or GetKeyState(Keys.Insert).ToString <> "-128"
    If GetKeyState(Keys.Insert).ToString = "-127" Or GetKeyState(Keys.Insert).ToString = "-128" Then
        PosMouse.Text = "OK"
        Exit Sub
    End If
End While

I have 2 programs: listen.exe and main.exe

Pressing a button in the listen.exe program will activate the loop so that when you press "INSERT" in the main.exe program, the position of the mouse will be recorded.

With what I can move the mouse to that position later.

I got out of trouble with a timer but I'm curious about the solution ...

Greetings and thank you very much!

    
asked by Adrian Hernando Solanas 19.05.2018 в 21:42
source

1 answer

2

The problem with that loop is that it is possible to block the ui, so that the form events are not processed, so that the keystroke will never be read.

A very simple solution to check if this is to put a Application.DoEvents() in the loop:

While GetKeyState(Keys.Insert).ToString <> "-127" Or GetKeyState(Keys.Insert).ToString <> "-128"
    If GetKeyState(Keys.Insert).ToString = "-127" Or GetKeyState(Keys.Insert).ToString = "-128" Then
        PosMouse.Text = "OK"
        Exit Sub
    End If
    Application.DoEvents()
End While

But the normal thing is that that code is in another thread, using BackGroundWorker or Task

    
answered by 21.05.2018 / 09:09
source