Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form following cursor until key press 1

Status
Not open for further replies.

Dalie

Programmer
Feb 16, 2009
17
0
0
NL

[concept]
i'm trying to create a form that follows - at a fixed position (x = 100px, y = 100px) - the cursor until a key (e.g. windows key + c) gets pressed. as soon as the key (combination) is pressed the program stops 'following' the cursor, the program becomes the 'active window' (in focus). (you are able to push a button inside the form). after clicking the button or pushing the 'windows key + c' again the form starts following the cursor again. the program is alway on top.
[\concept]

[questions]
i'm fairly new with vb2008, therefore the problem is putting the pieces code together.. i figured out most of the "functions" the program must be able to execute.. one thing is missing though: the form following the cursor coordinates at a fixed position, i searched and found some directions.. but i really can't figure out how i could combine them together.. maybe some of you have some suggestions where to look.. any help is appreciated!
[\questions]

i can post the code i have so far if needed..
 
I don't know this is the best way, but here is how I might do it.

Code:
Imports System.Threading

Public Class Form1
    Private FollowThread As Thread

    Private Sub Form1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
        If e.KeyCode = Keys.F Then
            If FollowThread IsNot Nothing Then
                FollowThread.Abort()
                FollowThread = Nothing
            Else
                FollowThread = New Thread(AddressOf FollowMouse)
                FollowThread.Name = "MouseFollowThread"
                FollowThread.Start()
            End If
        End If
    End Sub

    Private Sub FollowMouse()
        Do While FollowThread IsNot Nothing
            If Me.Location <> MousePosition Then
                Me.BeginInvoke(New delSetFormLocation(AddressOf SetFormLocation))
            End If
        Loop
    End Sub

    Private Delegate Sub delSetFormLocation()
    Private Sub SetFormLocation()
        Me.Location = MousePosition
    End Sub
End Class

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Oops, forgot to mention the forms KeyPreview Property has to be set to true. Also keep in mind if you allow them to type on the form you will want to use something other than the F key. You might use a Function key instead.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Woaw, that looks great! Many thanks! I changed the TopMost property to True so it stays on top of other applications. It works like a charm. However as soon as I select another program or select a text the program loses it's focus.

I'm trying to figure a neat way to get the focus back, because losing the focus the program won't respond to the "f"...

Many thanks again!

(I read some things about clicking through a form...)
 
A neat way would be using another thread that checks the "f" being pressed. Some sort of "boss" button, no mather what program is in focus, by pressing the "f" my program that is always on top comes to focus.. I couldn't find a way because I didn't had the time jet, any suggestions are welcome!

Again many thanks for all the support I get here!
 
That isn't a very good idea because you never know what buttons your other programs might need you to press. As to the loosing focus I didn't have that problem. You could try something like this:

Code:
    Private Sub Form1_MouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.MouseHover
        If FollowThread IsNot Nothing Then
            Me.Focus()
        End If
    End Sub
I can't test it because I don't have the focus problem. Mine actually works exactly as you seem to want yours to work if I set it to TopMost, and follows in the background if I don't. MouseHover should allow you a little time so that way it doesn't pick right back up. That way it should only pick up when you intend it to rather than just every time and possibly get in your way while you are working. I'm not sure if setting the focus will have it pick back up correctly or not. If for some reason that doesn't work or not in the way you want try the forms MouseEnter event.

Maybe it is a version difference. What Visual Studio are you using?

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I use VS2008, and I lose focus by pressing (alt + tab). For exercise reasons I still try to create a "boss" key. Which sets the focus. I came across some code, but I can't figure out where things go wrong... In this case it shows/hides the form.

Code:
Imports System.Threading

Private Declare Auto Function GetAsyncKeyState Lib "user32" Alias "GetAsyncKeyState" (ByVal key As Keys) As Short

    Dim bStopRunning As Boolean

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim t As New Thread(AddressOf ThreadProc)
        t.Start()
    End Sub

    Private Sub ThreadProc()
        While Not bStopRunning
            If GetAsyncKeyState(Keys.F5) And &H8000 Then
                Me.Visible = Not Me.Visible
            End If
            Thread.Sleep(150)
        End While
    End Sub

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        bStopRunning = True
    End Sub
 
hmmm....I tried that and it didn't do that to me before, but today when I have it follow some times it does have the same problem. It seems to be something to do with the TopMost because if I turn that off it never has a problem regaining focus.

Let me ask this, what is the purpose/end result you are wanting for this? There are likely to be several continued problems with this and to have a better understanding of what you ultimately want to do with this might help.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top