Solved

CHECK FORM KEY DOWN

Posted on 2013-06-18
14
320 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now