jimmy89 0 Report post Posted December 21, 2006 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!! Share this post Link to post Share on other sites
faulty.lee 0 Report post Posted December 21, 2006 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 focusFor 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 Share this post Link to post Share on other sites
jimmy89 0 Report post Posted December 21, 2006 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? Share this post Link to post Share on other sites
faulty.lee 0 Report post Posted December 21, 2006 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? 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 Share this post Link to post Share on other sites
jimmy89 0 Report post Posted December 21, 2006 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? Share this post Link to post Share on other sites
borlafu 0 Report post Posted December 21, 2006 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. Share this post Link to post Share on other sites
jimmy89 0 Report post Posted December 21, 2006 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 itSelect Case e.KeyCode Case Keys.F1 frmNew.MdiParent = Me frmNew.show() Exit SubEnd Select the exit sub should exit the sub that I am in, but when i try and press F1 again, i get nothing appearing! Share this post Link to post Share on other sites
faulty.lee 0 Report post Posted December 21, 2006 This is how my code looks after i edited it Select Case e.KeyCode Case Keys.F1 frmNew.MdiParent = Me frmNew.show() Exit SubEnd 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. Share this post Link to post Share on other sites
jimmy89 0 Report post Posted December 22, 2006 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? Share this post Link to post Share on other sites
jimmy89 0 Report post Posted December 22, 2006 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. Share this post Link to post Share on other sites
faulty.lee 0 Report post Posted December 22, 2006 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. If you're stepping through the code, very likely that your frmMain will be out of focus. One way is try not to switch focus when the debugger steps in, then the focus sequence will be switch back after you continue the code execution.Btw, frmMain should be in focus in order to receive the event. Is your child form really configured as MDI child? if not it will actually steal the focus also. Share this post Link to post Share on other sites
jimmy89 0 Report post Posted December 22, 2006 If you're stepping through the code, very likely that your frmMain will be out of focus. One way is try not to switch focus when the debugger steps in, then the focus sequence will be switch back after you continue the code execution.Btw, frmMain should be in focus in order to receive the event. Is your child form really configured as MDI child? if not it will actually steal the focus also. what do you mean by is your child form really configured? would i be able to use your class that you made for what im trying to do? Share this post Link to post Share on other sites
faulty.lee 0 Report post Posted December 22, 2006 what do you mean by is your child form really configured? would i be able to use your class that you made for what im trying to do? That might complicate things. It won't help or could make thing worse.How you add your child form to the frmMain? For me i use frmMain.Controls.Add(frmChild) when you initialize your child form. That way, the child won't appear as a separate form, and stealing the focus. There could be other way to do that which i'm not aware of. Share this post Link to post Share on other sites
jimmy89 0 Report post Posted December 22, 2006 i use the following code frmChild.MdiParent = mefrmChild.show but when i try to use your code i get a 'ArgumentException was unhgandled' error. 'Top-Level control cannot be added to a control'. what does this mean exactly? Share this post Link to post Share on other sites
faulty.lee 0 Report post Posted December 22, 2006 i use the following code frmChild.MdiParent = mefrmChild.show but when i try to use your code i get a 'ArgumentException was unhgandled' error. 'Top-Level control cannot be added to a control'. what does this mean exactly? I don't think that's enough to add it to the parentform, and i never use that. Btw, where did you put that 2 line of code? For my case, i put this in the frmMain frmMain.Controls.Add(frmChild) You should do the initialization before adding the control to the parent, including calling MdiParent = me. How did you child form appear with respect to the parent form? If it appear just like another normal form or dialog, then it might not have been made a child. Share this post Link to post Share on other sites