Hello everyone!

I need to make a simple yet cool program for my professor.

The program is this:
To get the program to work, the user first presses a button on the form.
Then, when the user presses an up arrow on the keyboard, the cursor will change to a custom cursor (a cursor with an image of car {in .ico format} for example), when the user releases the up arrow key, the cursor turns back to default cursor -- which is arrow. When the user presses an up arrow and left arrow at the same time on the keyboard, then a different custom cursor will be shown instead of the default.
The program must work even while the form is not in focus (not selected - like minimized on the screen) and when you are working in a different program like having a presentation in PowerPoint.

If you also know how to make a custom cursor and the coding for it to work as mentioned above, I will appreciate your dedication for helping me.

If you know the coding to make this simple program, please reply in clear and understandable letter.

Thanks a lot.
Your Humble Maximilian.

Recommended Answers

All 15 Replies

You will need to enable KeyPreview in the form. You can trap the up arrow press in the forms KeyUp event. You have to enable KeyPreview at design time. There is no way to set it at run time so you will have to have a global variable (call it something like filterKeys) and set it to True in the button click event. Then in your KeyUp handler you can do

If filterKeys Then
    .
    .
    .
End If

What have you got so far?

What I know so far is that you need to use the KeyDown event to change the cursor, and KeyUp when the user releases the key; this returns the cursor back to default cursor. But the Up, Down, Left, and Right buttons (UP, DOWN, LEFT, RIGHT Arrows on key the Board) don't change the cursor after the user clicked on the button in the window form to activate the cursor option, instead the arrow buttons are working like the TAB button. I also don’t know the code when the user presses down two keys at the same time, for the cursor to change. And I don’t know how to make a custom cursor and the code for it to work.

You will need to enable KeyPreview in the form. You can trap the up arrow press in the forms KeyUp event. You have to enable KeyPreview at design time. There is no way to set it at run time so you will have to have a global variable (call it something like filterKeys) and set it to True in the button click event. Then in your KeyUp handler you can do

If filterKeys Then
    .
    .
    .
End If

So I did like you said, changed the KeyPreview in the form to true, but after that point I don’t understand what to do. What do I declare the filterKeys as? And how do I set it to True in the button click event, like give me an example of the code.
Thanks, I'm just a beginner at this new language.

Dim filterKeys as Boolean
.
.
.
'in the ButtonClick event handler

filterKeys = True

However, if you don't know what a boolean is or how to set it then I suggest you do a lot more reading before you dive in.

See if this helps to get you started.

Public Class Form1

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyData
            Case Keys.Up
                Me.Cursor = Cursors.WaitCursor
        End Select
    End Sub

    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        Select Case e.KeyData
            Case Keys.Up
                Me.Cursor = Cursors.Default
        End Select
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.KeyPreview = True '// needed to get the keys.Focus.
    End Sub
End Class
Dim filterKeys as Boolean
.
.
.
'in the ButtonClick event handler

filterKeys = True

However, if you don't know what a boolean is or how to set it then I suggest you do a lot more reading before you dive in.

OK, so I have this done:

Dim filterKeys As Boolean

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        filterKeys = True
    End Sub

And I know that a boolean is true, false variables.

So how will I apply the boolean to the KeyUp event?
How is the boolean statement need to look like???

Private Sub Button1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button1.KeyUp
        If e.KeyCode = Keys.Up Then
            Me.Cursor = Cursors.Default
        End If

   If filterKeys Then
       e.KeyCode = Keys.Up = True
        End If
    End Sub

See if this helps to get you started.

Public Class Form1

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyData
            Case Keys.Up
                Me.Cursor = Cursors.WaitCursor
        End Select
    End Sub

    Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        Select Case e.KeyData
            Case Keys.Up
                Me.Cursor = Cursors.Default
        End Select
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.KeyPreview = True '// needed to get the keys.Focus.
    End Sub
End Class

Can you go through the process of making a custom cursor?

If e.KeyCode = Keys.Up Then Me.Cursor = Cursors.Default

   If filterKeys Then If e.KeyCode = Keys.Up Then Me.Cursor = Cursors.WaitCursor

'// note: did not test.

>>Can you go through the process of making a custom cursor?
Drawing it? Cropping it? Adding colors to it? Or switching the cursor to a custom.cursor?

If e.KeyCode = Keys.Up Then Me.Cursor = Cursors.Default

   If filterKeys Then If e.KeyCode = Keys.Up Then Me.Cursor = Cursors.WaitCursor

'// note: did not test.

But where is the code for my custom cursor that I want to show?
I know it should start like this:

Me.Cursor = System.Windows.Forms.Cursor namespace.

>>But where is the code for my custom cursor that I want to show?
See if this helps. :)

If you need further help, do let me know. I might find another search.engine other than "LMGTFY".

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.KeyPreview = True '// needed to get the keys.Focus.
End Sub

In my version of Visual Studio (2010) you can't use Me.KeyPreview which is why I suggested setting it at design time and using a user defined boolean.

If you have the following at the top of your Class

Dim mycursors() As Cursor = {Cursors.AppStarting, Cursors.Arrow, Cursors.Cross}
Dim cursorindex As Integer = -1

Then in your KeyUp handler you can do

If filterkeys Then
    cursorindex = (cursorindex + 1) Mod (UBound(mycursors) + 1)
    Cursor = mycursors(cursorindex)
End If

For the sake of simplicity (also because I am lazy) I only included three of the built-in cursor types. You can add to the list and the code will still work. Note that the code in the KeyUp handler cycles through all cursors in the array, mycursors. The Mod function sets the index back to zero if it points past the end of the array.

In my version of Visual Studio (2010) you can't use Me.KeyPreview which is why I suggested setting it at design time and using a user defined boolean.

If you have the following at the top of your Class

Dim mycursors() As Cursor = {Cursors.AppStarting, Cursors.Arrow, Cursors.Cross}
Dim cursorindex As Integer = -1

Then in your KeyUp handler you can do

If filterkeys Then
    cursorindex = (cursorindex + 1) Mod (UBound(mycursors) + 1)
    Cursor = mycursors(cursorindex)
End If

For the sake of simplicity (also because I am lazy) I only included three of the built-in cursor types. You can add to the list and the code will still work. Note that the code in the KeyUp handler cycles through all cursors in the array, mycursors. The Mod function sets the index back to zero if it points past the end of the array.

Hey. I thought you were talking about the key down event, because when I click on the button on the form with the code for key down like this:

If e.KeyCode = Keys.Up Then      
      Me.Cursor = Cursors.Cross
    End If

The cursor doesnt change. But if I use another key instead of Up, like the F button, the cursor does change. How do I resolve this?

Sorry about that. A clearer bit of code would be

Imports System.Windows.Forms

Public Class Form1

    Private filterkeys As Boolean = False
    Private mycursors() As Cursor = {Cursors.AppStarting, Cursors.Arrow, Cursors.Cross}
    Private cursorindex As Integer = -1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        filterkeys = True
    End Sub

    Private Sub Form1_KeyUp(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp
        If filterkeys And e.KeyCode = Keys.Up Then
            cursorindex = (cursorindex + 1) Mod (UBound(mycursors) + 1)
            Cursor = mycursors(cursorindex)
        End If
    End Sub

End Class

Don't confuse Form1_KeyUp with Keys.Up. "KeyUp" in the function name refers to the event "when a key is released (the UP event)" with "Keys.Up" which refers to the key code for the UP key.

PS - I've been programming for years but there are many many people here who are more adept than I am in VB and object oriented programming so when I suggest a solution keep in mind that it is "a" solution, not necessarily the best or only solution ;-)

commented: good stuff: "Don't confuse Form1_KeyUp with Keys.Up..." :) +12
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.