Results 1 to 11 of 11

Thread: Send Keys without SendKeys

  1. #1

    Thread Starter
    Hyperactive Member Frodo_Baggins's Avatar
    Join Date
    Feb 2004
    Location
    Sao Paulo, Brazil
    Posts
    397

    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})
    Ash Nazg durbatuluk, Ash Nazg gimbatul, Ash Nazg tharkathuluk, Agh barzum-ishi krimpatul.

  2. #2
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    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.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  3. #3

    Thread Starter
    Hyperactive Member Frodo_Baggins's Avatar
    Join Date
    Feb 2004
    Location
    Sao Paulo, Brazil
    Posts
    397

    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?
    Ash Nazg durbatuluk, Ash Nazg gimbatul, Ash Nazg tharkathuluk, Agh barzum-ishi krimpatul.

  4. #4
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Send Keys without SendKeys

    This works!! Set the timer's interval to 100 or so.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    4.  
    5. Private Sub submnuOpen_Click()
    6.     MsgBox "Open Clicked!!"
    7. End Sub
    8.  
    9. Private Sub tmrKeyPress_Timer()
    10.     If GetAsyncKeyState(vbKeyF3) <> 0 Then
    11.         MsgBox "F3 was pressed!!"
    12.     End If
    13. End Sub
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  5. #5

    Thread Starter
    Hyperactive Member Frodo_Baggins's Avatar
    Join Date
    Feb 2004
    Location
    Sao Paulo, Brazil
    Posts
    397

    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?
    Ash Nazg durbatuluk, Ash Nazg gimbatul, Ash Nazg tharkathuluk, Agh barzum-ishi krimpatul.

  6. #6
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    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.
    Attached Files Attached Files
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  7. #7

    Thread Starter
    Hyperactive Member Frodo_Baggins's Avatar
    Join Date
    Feb 2004
    Location
    Sao Paulo, Brazil
    Posts
    397

    Re: Send Keys without SendKeys

    Ok! Thanks... I'll take a look.
    Ash Nazg durbatuluk, Ash Nazg gimbatul, Ash Nazg tharkathuluk, Agh barzum-ishi krimpatul.

  8. #8

    Thread Starter
    Hyperactive Member Frodo_Baggins's Avatar
    Join Date
    Feb 2004
    Location
    Sao Paulo, Brazil
    Posts
    397

    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...
    Ash Nazg durbatuluk, Ash Nazg gimbatul, Ash Nazg tharkathuluk, Agh barzum-ishi krimpatul.

  9. #9
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    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:
    1. Option Explicit
    2.  
    3. Private Sub submnuOpen_Click()
    4.     MsgBox "Open Clicked!!"
    5. End Sub
    6.  
    7. Public Sub Form_KeyPress(KeyAscii As Integer)
    8.     Text1.Text = KeyAscii
    9.    
    10.     If KeyAscii = vbKeyF3 Then
    11. '        MsgBox "F3 was pressed!!"
    12.         submnuOpen_Click
    13.     End If
    14. End Sub
    15.  
    16. Private Sub Form_Load()
    17.     Form1.Show
    18. End Sub

    If this is not what you want, you might explain better what you need. There might be another work-around.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  10. #10

    Thread Starter
    Hyperactive Member Frodo_Baggins's Avatar
    Join Date
    Feb 2004
    Location
    Sao Paulo, Brazil
    Posts
    397

    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.
    Ash Nazg durbatuluk, Ash Nazg gimbatul, Ash Nazg tharkathuluk, Agh barzum-ishi krimpatul.

  11. #11
    Fanatic Member
    Join Date
    Mar 2002
    Location
    AUSTRALIA
    Posts
    603

    Re: Send Keys without SendKeys

    There may be a complexity that I am not seeing ?, but re -
    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.
    Rob C

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width