Nov 22, 2009
Pages: 1, 2, 3

Keyboard Buttons

free web hosting

Read Latest Entries..: (Post #28) by iGuest on Oct 25 2007, 06:47 AM.
Name:Basel OlayyanEmail:basel_baselll@hotmail.comOpinion/*: Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown Select Case e.KeyCode Case Keys.F1 MessageBox.Show("F1") Case Keys.F5 MessageBox.Show("F5") Case Keys.F6 AndAlso e.Shift 'Shift + F6 End Select End SubI have used this code but when I but ant button or ...
read more.
Read the FIRST post of this Topic. - Express your Opinion! Contribute Knowledge :-).

Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > BASIC / Visual Basic (.NET)

Keyboard Buttons

Jimmy89
Hi, i am trying to make a program that uses keyboard combinations to run events in the program. like pressing F1 for help or CTRL + ALT + DEL for Task Manager. All i need to know is what code do i need to detect the keyboard events and do i have to use the KeyPress event on the form?? THANKS!!

Comment/Reply (w/o sign-up)

faulty.lee
QUOTE(Jimmy89 @ Dec 21 2006, 02:50 PM) *

Hi, i am trying to make a program that uses keyboard combinations to run events in the program. like pressing F1 for help or CTRL + ALT + DEL for Task Manager. All i need to know is what code do i need to detect the keyboard events and do i have to use the KeyPress event on the form?? THANKS!!


For F1 and those non system key combination, you can use KeyPress or KeyDown event. Keydown wuld be better, since it send the event as soon as the key is pressed. keydown need to wait for the key to be release before sending the event. And this only works when the form has the focus

For system key combination like ctrl+alt+del, you'll need to use SetWindowsHookEx and WH_KEYBOARD_LL. Unless you have previous experience in calling external dll and using callback function. I don't quite recommend using this, as it might cause more problem than you originally faced. If you really want, i've a pre built class(took me more than 1 month to make it work) for this. I can post the example here. It will only works in win2k and above. I need it cause i need to detect keypress even when the form is does not have focus or when i app is using tray icon

 

 

 


Comment/Reply (w/o sign-up)

Jimmy89
thanks for that, but how exactly do i use the keydown event? what code do i have to put in there for it to recognize when i press the F5 key for example?

Comment/Reply (w/o sign-up)

faulty.lee
QUOTE(Jimmy89 @ Dec 21 2006, 04:28 PM) *

thanks for that, but how exactly do i use the keydown event? what code do i have to put in there for it to recognize when i press the F5 key for example?


CODE

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Select Case e.KeyCode
            Case Keys.F1
                'F1
            Case Keys.F5
                'F5
            Case Keys.F6 AndAlso e.Shift
                'Shift + F6
        End Select
    End Sub


you can also use If...else...end if. I prefer select cause easier to add more later on

Comment/Reply (w/o sign-up)

Jimmy89
thanks for that! there is a problem though, when i run the program and press F1 it coded it just to show a simple Message Box. It works the first time, but when i try again it will not work! is there something i'm missing?

Comment/Reply (w/o sign-up)

borlafu
Visual Basic is "event oriented", it should work every time you want using the code before. Maybe you don't finish the event handler correctly so it cannot start another event.

Comment/Reply (w/o sign-up)

Jimmy89
QUOTE(borlafu @ Dec 21 2006, 08:25 PM) *

Visual Basic is "event oriented", it should work every time you want using the code before. Maybe you don't finish the event handler correctly so it cannot start another event.

This is how my code looks after i edited it
CODE

Select Case e.KeyCode
            Case Keys.F1
                frmNew.MdiParent = Me
                frmNew.show()
                Exit Sub
End Select


the exit sub should exit the sub that I am in, but when i try and press F1 again, i get nothing appearing!

Comment/Reply (w/o sign-up)

faulty.lee
QUOTE(Jimmy89 @ Dec 21 2006, 05:36 PM) *

This is how my code looks after i edited it
CODE

Select Case e.KeyCode
            Case Keys.F1
                frmNew.MdiParent = Me
                frmNew.show()
                Exit Sub
End Select


the exit sub should exit the sub that I am in, but when i try and press F1 again, i get nothing appearing!


Since you use show(), the form might be minimized or it doesn't get focus. Or could be an exception. You can try add and break point at ->Select, then step through the code when you press the F1. That way you can find out what's wrong.

Comment/Reply (w/o sign-up)

Jimmy89
i tried a breakpoint at the 'select case e.KeyCode' line and stepped through each line one by one and as i went through it highlighted each step in the case, then end select, then end sub. is this what it was supposed to do?

Comment/Reply (w/o sign-up)

Jimmy89
Hey, i worked out what the problem is. each time i hit a keyboard button and raise an event, the frmMain becomes unfocused. how am i able to refocus the form? (I tried the me.focus event but that doesn't seem to work)

also, in case it is any help this frmMain is a MDI Parent form.

Comment/Reply (w/o sign-up)

Latest Entries

iGuest
Name:Basel Olayyan
Email:basel_baselll@hotmail.com
Opinion/*: Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Select Case e.KeyCode
Case Keys.F1
MessageBox.Show("F1")
Case Keys.F5
MessageBox.Show("F5")
Case Keys.F6 AndAlso e.Shift
'Shift + F6
End Select
End Sub

I have used this code but when I but ant button or text box or any thing on the form this even not working and I don't know wht to do Plz if you have any Suggestion how to do it it well be appreciated
and thx for your help


-Basel Olayyan

Comment/Reply (w/o sign-up)

Jimmy89
thanks! i'll look into it!

Comment/Reply (w/o sign-up)

faulty.lee
QUOTE(Jimmy89 @ Jan 4 2007, 07:56 AM) *

Thanks for your help! changing the key preview property seems to have fixed the problem. Also, thanks for the code fixes, they all made sense and i have changed my code too.


Glad it helps. Keep up

EDIT: Try to read some books on this, it help understand more bout the basic of it, that way you can program more efficiently

Comment/Reply (w/o sign-up)

Jimmy89
Thanks for your help! changing the key preview property seems to have fixed the problem. Also, thanks for the code fixes, they all made sense and i have changed my code too.

Comment/Reply (w/o sign-up)

faulty.lee
Hi,

Ok, i've tested. Just this code you posted earlier works fine for me

CODE

frmChild.MdiParent = me
frmChild.show


Sorry for giving you the wrong info on Controls.Add(). I seldom use MDI for my software. I find it confusing.
Anyway, you also need to enable "KeyPreview" (Misc) for your main form for the keyboard event to work properly.

After seeing you code, i do have a few comment for you
CODE

Dim response As Microsoft.VisualBasic.MsgBoxResult


Use this instead of interger, that way your code of
CODE

                If response = 6 Then


can be written as

CODE

                If response = MsgBoxResult.Yes Then


which is a lot of easier to read and debug

The 2nd thing is try to use as much OOP strategy as possible. One of it is keep all this code that show your MDI Child in side one function, then call the function from those menu and key event. That way, it's easier to maintain the code. If you need to change the way your Help Windows appear, just change that function. All those event that call the function gets the changes. f not, you'll have to look through all your codes to make the changes, and you might miss it, trust me, it does, all the time

Instead of
CODE

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Dim response As Microsoft.VisualBasic.MsgBoxResult

        Select Case e.KeyCode
            Case Keys.F1
                MsgBox("You asked for help")
                Exit Sub
            Case Keys.F2
                frmWebHelp.MdiParent = Me
                frmWebHelp.Show()
..... more code

    Private Sub WebHelpToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WebHelpToolStripMenuItem.Click
        frmWebHelp.MdiParent = Me
        frmWebHelp.Show()
    End Sub



You change it to

CODE

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        Dim response As Microsoft.VisualBasic.MsgBoxResult

        Select Case e.KeyCode
            Case Keys.F1
                MsgBox("You asked for help")
                Exit Sub
            Case Keys.F2
                ShowfrmWebHelp()
..... more code

    Private Sub WebHelpToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WebHelpToolStripMenuItem.Click
        ShowfrmWebHelp()
    End Sub

    Private Sub ShowfrmWebHelp()
        frmWebHelp.MdiParent = Me
        frmWebHelp.Show()
    End Sub



VB.Net implemented a lot of OOP features compare to VB6, it's totally re-engineered, thus why not use that to our fullest

All the best

Regards
Faulty

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Pages: 1, 2, 3
Similar Topics

Keywords : Keyboard Buttons


    Looking for keyboard, buttons

See Also,

*SIMILAR VIDEOS*
Searching Video's for keyboard, buttons
advertisement



Keyboard Buttons

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com