Send Keys without SendKeys
Hello.
I have two active forms in my app, the Main one and another one. I would like to have the program transmit the keypresses from the other form to the Main one when it (the other form) is active.
Say, in the Main form, F3 triggers a specific event. I would like to have the Main form know that F3 has been pressed when it has no focus (when focus is on the other form).
Sendkeys COULD work, but for special keys (such as Enter) there would be trouble... (have to associate for example 13 to {ENTER})
Re: Send Keys without SendKeys
Quote:
Originally Posted by Frodo_Baggins
Hello.
I have two active forms in my app, the Main one and another one. I would like to have the program transmit the keypresses from the other form to the Main one when it (the other form) is active.
Say, in the Main form, F3 triggers a specific event. I would like to have the Main form know that F3 has been pressed when it has no focus (when focus is on the other form).
Sendkeys COULD work, but for special keys (such as Enter) there would be trouble... (have to associate for example 13 to {ENTER})
In your other form, set the KeyPreview property to true. Then add a call in the KeyPress of your other form to inform the Main form which key was pressed.
Re: Send Keys without SendKeys
Ohh... hum... i feared that answer.
Actually, I thought about that...
But the keys are actually associated to Menus (in the Menu Editor), and not by stuff in the KeyPress Sub of the Main form...
It wouldn't work then, would it?
Re: Send Keys without SendKeys
This works!! Set the timer's interval to 100 or so.
VB Code:
Option Explicit
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub submnuOpen_Click()
MsgBox "Open Clicked!!"
End Sub
Private Sub tmrKeyPress_Timer()
If GetAsyncKeyState(vbKeyF3) <> 0 Then
MsgBox "F3 was pressed!!"
End If
End Sub
Re: Send Keys without SendKeys
HUm... yes... but isn't there a way to do that without having to detect each key individually?
Like automatically sending the same keycode to the Main form?
1 Attachment(s)
Re: Send Keys without SendKeys
Quote:
Originally Posted by Frodo_Baggins
HUm... yes... but isn't there a way to do that without having to detect each key individually?
Like automatically sending the same keycode to the Main form?
Have a look at the attach.
Re: Send Keys without SendKeys
Ok! Thanks... I'll take a look.
Re: Send Keys without SendKeys
Hum.... Actually, the menu is supposed to be on the Main form...
It won't work... I switched the menu to the main form, but now the only event that is triggered is the KeyPress, and not the menu click.....
Triggering the KeyPress event just won't work for me...
Re: Send Keys without SendKeys
Quote:
Originally Posted by Frodo_Baggins
Hum.... Actually, the menu is supposed to be on the Main form...
It won't work... I switched the menu to the main form, but now the only event that is triggered is the KeyPress, and not the menu click.....
Triggering the KeyPress event just won't work for me...
I know this is not what you might be looking for... This goes into your Main.frm:
VB Code:
Option Explicit
Private Sub submnuOpen_Click()
MsgBox "Open Clicked!!"
End Sub
Public Sub Form_KeyPress(KeyAscii As Integer)
Text1.Text = KeyAscii
If KeyAscii = vbKeyF3 Then
' MsgBox "F3 was pressed!!"
submnuOpen_Click
End If
End Sub
Private Sub Form_Load()
Form1.Show
End Sub
If this is not what you want, you might explain better what you need. There might be another work-around.
Re: Send Keys without SendKeys
Yeah.... I didn't want to have to add that code for every menu item...
Well, if there is no other way, I guess I'll have to do that then...
Thanks for the help.
Re: Send Keys without SendKeys
There may be a complexity that I am not seeing ?, but re -
Quote:
Say, in the Main form, F3 triggers a specific event. I would like to have the Main form know that F3 has been pressed when it has no focus (when focus is on the other form).
Can't you just have a public property in the Main form.
And within that property you take certain actions depening on what was sent to that property.
Thus when F3 is pressed in the other form, it sends that info to the Main form's public property.