Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

CHECK FORM KEY DOWN

Posted on 2013-06-18
14
Medium Priority
?
351 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline

876 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