Solved

CHECK FORM KEY DOWN

Posted on 2013-06-18
14
318 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

707 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

14 Experts available now in Live!

Get 1:1 Help Now