Jump to content



Welcome to AstaHost - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
Photo
- - - - -

Keyboard Buttons


28 replies to this topic

#1 Jimmy89

Jimmy89

    Living at the Datacenter

  • [HOSTED]
  • 713 posts
  • Gender:Male
  • Location:Australia
  • myCENTs:14.01

Posted 21 December 2006 - 06:50 AM

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

#2 faulty.lee

faulty.lee

    Super Member

  • [HOSTED]
  • 500 posts
  • Interests:Electronics, Software Programming, Embedded Programming, Movies, Windows Shopping
  • myCENTs:79.88

Posted 21 December 2006 - 08:17 AM

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

#3 Jimmy89

Jimmy89

    Living at the Datacenter

  • [HOSTED]
  • 713 posts
  • Gender:Male
  • Location:Australia
  • myCENTs:14.01

Posted 21 December 2006 - 08:28 AM

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?

#4 faulty.lee

faulty.lee

    Super Member

  • [HOSTED]
  • 500 posts
  • Interests:Electronics, Software Programming, Embedded Programming, Movies, Windows Shopping
  • myCENTs:79.88

Posted 21 December 2006 - 08:32 AM

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

#5 Jimmy89

Jimmy89

    Living at the Datacenter

  • [HOSTED]
  • 713 posts
  • Gender:Male
  • Location:Australia
  • myCENTs:14.01

Posted 21 December 2006 - 09:06 AM

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?

#6 borlafu

borlafu

    Member [ Level 2 ]

  • Members
  • 58 posts

Posted 21 December 2006 - 09:25 AM

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.

#7 Jimmy89

Jimmy89

    Living at the Datacenter

  • [HOSTED]
  • 713 posts
  • Gender:Male
  • Location:Australia
  • myCENTs:14.01

Posted 21 December 2006 - 09:36 AM

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

#8 faulty.lee

faulty.lee

    Super Member

  • [HOSTED]
  • 500 posts
  • Interests:Electronics, Software Programming, Embedded Programming, Movies, Windows Shopping
  • myCENTs:79.88

Posted 21 December 2006 - 02:01 PM

This is how my code looks after i edited it

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.

#9 Jimmy89

Jimmy89

    Living at the Datacenter

  • [HOSTED]
  • 713 posts
  • Gender:Male
  • Location:Australia
  • myCENTs:14.01

Posted 22 December 2006 - 12:09 AM

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?

#10 Jimmy89

Jimmy89

    Living at the Datacenter

  • [HOSTED]
  • 713 posts
  • Gender:Male
  • Location:Australia
  • myCENTs:14.01

Posted 22 December 2006 - 12:20 AM

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.

#11 faulty.lee

faulty.lee

    Super Member

  • [HOSTED]
  • 500 posts
  • Interests:Electronics, Software Programming, Embedded Programming, Movies, Windows Shopping
  • myCENTs:79.88

Posted 22 December 2006 - 03:21 AM

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.

#12 Jimmy89

Jimmy89

    Living at the Datacenter

  • [HOSTED]
  • 713 posts
  • Gender:Male
  • Location:Australia
  • myCENTs:14.01

Posted 22 December 2006 - 06:24 AM

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?

#13 faulty.lee

faulty.lee

    Super Member

  • [HOSTED]
  • 500 posts
  • Interests:Electronics, Software Programming, Embedded Programming, Movies, Windows Shopping
  • myCENTs:79.88

Posted 22 December 2006 - 09:38 AM

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.

#14 Jimmy89

Jimmy89

    Living at the Datacenter

  • [HOSTED]
  • 713 posts
  • Gender:Male
  • Location:Australia
  • myCENTs:14.01

Posted 22 December 2006 - 12:00 PM

i use the following code

frmChild.MdiParent = me
frmChild.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?

#15 faulty.lee

faulty.lee

    Super Member

  • [HOSTED]
  • 500 posts
  • Interests:Electronics, Software Programming, Embedded Programming, Movies, Windows Shopping
  • myCENTs:79.88

Posted 22 December 2006 - 03:28 PM

i use the following code

frmChild.MdiParent = me
frmChild.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.

#16 Jimmy89

Jimmy89

    Living at the Datacenter

  • [HOSTED]
  • 713 posts
  • Gender:Male
  • Location:Australia
  • myCENTs:14.01

Posted 22 December 2006 - 11:59 PM

Select Case e.KeyCode
			
			Case Keys.F2
				frmChild.MdiParent = Me
				frmChild.Show()
				Me.Controls.Add(frmChild)

			End Select

this code still gives me same error as before

Edited by Jimmy89, 22 December 2006 - 11:59 PM.


#17 faulty.lee

faulty.lee

    Super Member

  • [HOSTED]
  • 500 posts
  • Interests:Electronics, Software Programming, Embedded Programming, Movies, Windows Shopping
  • myCENTs:79.88

Posted 23 December 2006 - 03:18 AM

Select Case e.KeyCode
			
			Case Keys.F2
				frmChild.MdiParent = Me
				frmChild.Show()
				Me.Controls.Add(frmChild)

			End Select

this code still gives me same error as before


Controls.Add has to be call before you call "show()". And about the error, what error was it? I remember you were saying that it's not focus, but you never mention any error. You have to be clear with your term, or else others will get confuse. If you don't mind, can you explain a bit bout what your code is suppose to do, and so far what have been achieved and which part is not working as you intended it to. With an overview, it would be easier to solve you problem

#18 Jimmy89

Jimmy89

    Living at the Datacenter

  • [HOSTED]
  • 713 posts
  • Gender:Male
  • Location:Australia
  • myCENTs:14.01

Posted 26 December 2006 - 08:36 AM

				Me.Controls.Add(frmChild)
				frmChild.MdiParent = Me
				frmChild.Show()

when i use this code i get the following error:

ArgumentException was unhandled
Top-level control cannot be added to a control.

when i rearrange the code so that the declaration of the parent form comes before adding the control

		   Me.Controls.Add(frmChild)
		   frmChild.MdiParent = me
		   frmChild.show()

i get a different error:

ArgumentException was unhandled
Form cannot be added to the Controls collection that has a valid MDI parent.
Parameter name: value

#19 faulty.lee

faulty.lee

    Super Member

  • [HOSTED]
  • 500 posts
  • Interests:Electronics, Software Programming, Embedded Programming, Movies, Windows Shopping
  • myCENTs:79.88

Posted 26 December 2006 - 05:45 PM

Do You mind to zip your code and attach it here?

#20 Jimmy89

Jimmy89

    Living at the Datacenter

  • [HOSTED]
  • 713 posts
  • Gender:Male
  • Location:Australia
  • myCENTs:14.01

Posted 26 December 2006 - 11:44 PM

i have attached the code zipped. the frmChild that i have been referring to in the code is actually named frmWebHelp.

Attached Files





Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users