Solved

CHECK FORM KEY DOWN

Posted on 2013-06-18
14
321 Views
Last Modified: 2013-06-19
Hi All,

I have form and textbox keydown event.

The form event is fired first then textbox.

I want to check at form event if the keydown is Key F, Crtl, Alt, Shift.
I want to exit from the sub as soon as possible, otherwise it will continue to check those keys at other sub.

How could I do it ?

Thank you.
0
Comment
Question by:emi_sastra
  • 7
  • 7
14 Comments
 
LVL 11

Expert Comment

by:Deepak Lakkad
ID: 39258318
Hi

refer following code

 Dim nextSub As Boolean

    Private Sub TextBox1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If nextSub = False Then
            Exit Sub
        End If

        Debug.Print("Textbox Event")
    End Sub

    Private Sub Form1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = 70 And e.Shift = True Then
            nextSub = False
        Else
            nextSub = True
        End If
    End Sub

Open in new window


I have declared a variable nextSub at form level.

In form's KeyDown event, I check whether it is "F" with Shift key then I set value of nextSub to false.

And In KeyDown Event of Textbox, I check If nextSub is false, then exit from That Sub... Thus it will not execute the KeyDown of Textbox.


I think this will help you ...

- Deepak Lakkad
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39258428
Hi Deepak Lakkad,

I try :

 Private Sub frmMain_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown

        'Kalau bukan F, Ctrl dan Alt maka keluar

        If Not e.Shift Or _
            Not e.KeyCode = 70 Or _
            Not e.Alt Or _
            Not e.Control Or _
            Not e.KeyCode = Keys.Escape Then Exit Sub

        If e.KeyCode = Keys.Escape Then
            Dim intAnswer As Integer = MsgBox("Yakin Mau Keluar Dari Menu ...!", MsgBoxStyle.YesNo, Me.Text)
            If intAnswer = vbYes Then
                Me.Close()
                Exit Sub
            End If
        End If

        Me.Check_Keys(sender, e)

    End Sub

Anything wrong  ?

Thank you.
0
 
LVL 11

Expert Comment

by:Deepak Lakkad
ID: 39258634
Hi,

In your code,

If Not e.Shift Or _
            Not e.KeyCode = 70 Or _
            Not e.Alt Or _
            Not e.Control Or _
            Not e.KeyCode = Keys.Escape Then Exit Sub

Open in new window


will terminate the execute of sub if escape is pressed.

While following code will execute only if Escape is pressed.

 
If e.KeyCode = Keys.Escape Then
            Dim intAnswer As Integer = MsgBox("Yakin Mau Keluar Dari Menu ...!", MsgBoxStyle.YesNo, Me.Text)
            If intAnswer = vbYes Then
                Me.Close()
                Exit Sub
            End If
        End If

Open in new window


So, In this case, the second block will never execute ...

I think this is the problem in your code ...

- Deepak Lakkad
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 1

Author Comment

by:emi_sastra
ID: 39260118
Ok. I try :

Private Sub frmMain_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown

        'Kalau bukan F, Ctrl dan Alt maka keluar

        If e.KeyCode = Keys.Escape Then
            Dim intAnswer As Integer = MsgBox("Yakin Mau Keluar Dari Menu ...!", MsgBoxStyle.YesNo, Me.Text)
            If intAnswer = vbYes Then
                Me.Close()
                Exit Sub
            End If
        Else
            If Not e.Shift Or _
           Not e.KeyCode = 70 Or _
           Not e.Alt Or _
           Not e.Control Then Exit Sub
        End If

        Me.Check_Keys(sender, e)

    End Sub

When I press F keys, it goes to exit sub ?

What's wrong ?

Thank you.
0
 
LVL 11

Expert Comment

by:Deepak Lakkad
ID: 39261264
Hi

You are making mistake in write following condition

  If Not e.Shift Or _
           Not e.KeyCode = 70 Or _
           Not e.Alt Or _
           Not e.Control Then Exit Sub
        End If

I don't understand what do you want to do. Can you please explain your requirement so I can write few lines of code for it.

- Deepak Lakkad
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39261290
-I don't understand what do you want to do. Can you please explain your requirement so I can write few lines of code for it.
At form keydown event if not special keys (Shift, Ctrl, Alt, F(x) and Esc) I want to exit from that sub.

Thank you.
0
 
LVL 11

Expert Comment

by:Deepak Lakkad
ID: 39261316
Hi

For your requirement, If should be written as following


If e.KeyCode = Keys.F1 Or e.KeyCode = Keys.F2 Or e.KeyCode = Keys.F3 Or e.KeyCode = Keys.F4 Or e.KeyCode = Keys.F5 Or _
               e.KeyCode = Keys.F6 Or e.KeyCode = Keys.F7 Or e.KeyCode = Keys.F8 Or e.KeyCode = Keys.F9 Or e.KeyCode = Keys.F10 Or _
               e.Control Or e.Shift Or e.Alt Or e.KeyCode = Keys.Escape Then

                Exit Sub
            End If

Please check it

- Deepak Lakkad
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39261325
Ok. But I have to write down those F Keys ?

Below is your code :

 Private Sub Form1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = 70 And e.Shift = True Then
            nextSub = False
        Else
            nextSub = True
        End If
    End Sub

What is e.KeyCode = 70 means ?

Thank you.
0
 
LVL 11

Expert Comment

by:Deepak Lakkad
ID: 39261326
Hi


replace

 If e.KeyCode = 70 And e.Shift = True Then

with

newly given code

- or -

you can replace

If e.KeyCode = 70 And e.Shift = True Then
            nextSub = False
        Else
            nextSub = True
        End If
End if

with new code


- Deepak Lakkad
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39261361
If e.KeyCode = Keys.F1 Or e.KeyCode = Keys.F2 Or e.KeyCode = Keys.F3 Or e.KeyCode = Keys.F4 Or e.KeyCode = Keys.F5 Or _
               e.KeyCode = Keys.F6 Or e.KeyCode = Keys.F7 Or e.KeyCode = Keys.F8 Or e.KeyCode = Keys.F9 Or e.KeyCode = Keys.F10 Or _
               e.Control Or e.Shift Or e.Alt Or e.KeyCode = Keys.Escape Then

                Exit Sub
            End If

Should it be :

IF NOT (KeyCode .. ) THEN EXIT SUB

Since this is at form event.

Is it correct ?

Thank you.
0
 
LVL 11

Expert Comment

by:Deepak Lakkad
ID: 39261379
yes you are right ...
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 39261411
Is there any way to check just F key, does not have to write all that FKeys.

Thank you.
0
 
LVL 11

Accepted Solution

by:
Deepak Lakkad earned 500 total points
ID: 39261455
Change the if condition as shown below:

If (e.KeyCode >= Keys.F1 And e.KeyCode <= Keys.F12) Or _
               e.Control Or e.Shift Or e.Alt Or e.KeyCode = Keys.Escape Then

If will work same way

- Deepak Lakkad
0
 
LVL 1

Author Closing Comment

by:emi_sastra
ID: 39261462
Hi Deepak,

Thank you very much for your help.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question