Results 1 to 22 of 22

Thread: [RESOLVED] VB6 Form and PDF

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    14

    Resolved [RESOLVED] VB6 Form and PDF

    Don't know if this has been solved but I couldn't find the answers.

    After loading the PDF document using either webbrowser control or pdf.ocx, how do you capture the key down events?

    I can capture the key down events on the webbrowser control if I'm viewing a webpage.

    Or is there a way to transfer the focus from the pdf viewer to a button on the form without using a mouse?

    The application I'm updating does not allow the use of a mouse.

    Hope someone can help

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: VB6 Form and PDF

    I tried setting KeyPreview on the underlying form, but the WebBrowser control at least doesn't seem to respect it. Perhaps your pdf.ocx will. Alternatively, the WebBrowser exposes an hWnd, so you should be able to subclass it and catch keyboard events manually. This is unfortunate since it's complicated, but there are a bunch of threads on subclassing on these forums.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  3. #3
    Member
    Join Date
    Mar 2010
    Posts
    33

    Re: VB6 Form and PDF

    I have an alternative that I think is easier than subclassing. Use a keyboard hook and check if your application is the active one.

  4. #4
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: VB6 Form and PDF

    Unless you're using an external library, a keyboard hook is probably more complicated than subclassing. But it's certainly an alternative, as is KeyAsyncKeystate polling (which I strongly dislike).
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    14

    Re: VB6 Form and PDF

    Quote Originally Posted by jemidiah View Post
    I tried setting KeyPreview on the underlying form, but the WebBrowser control at least doesn't seem to respect it. Perhaps your pdf.ocx will. Alternatively, the WebBrowser exposes an hWnd, so you should be able to subclass it and catch keyboard events manually. This is unfortunate since it's complicated, but there are a bunch of threads on subclassing on these forums.
    Thanks for the reply. I tried the pdf.ocx control, I still can't get the key events.
    I also can't get the focus to transfer to a command button on the form whenever the focus is inside the pdf document.

    For example while viewing the pdf document, I want to press F5 to open a certain a command, but adobe reader catches the F5 key and displays the bookmarks tab on the left side.

    I'll research about hWnd and see what I can find.

  6. #6
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: VB6 Form and PDF

    "Subclassing" is the thing you really want to search for. As gigel_hifi mentioned, you could also look for "key hooks", but I'm not sure if you can squash key press propagation with that method. Probably. It's sort of like using a hammer when the back of a screwdriver will do.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    14

    Re: VB6 Form and PDF

    Quote Originally Posted by jemidiah View Post
    "Subclassing" is the thing you really want to search for. As gigel_hifi mentioned, you could also look for "key hooks", but I'm not sure if you can squash key press propagation with that method. Probably. It's sort of like using a hammer when the back of a screwdriver will do.

    I found some articles on key hooks.
    I tried this tutorial:
    Managing-Low-Level-Keyboard-Hooks-with-the-Windows-API

    Now my form knows when I've pressed a function key while viewing a pdf document using webbrowser control.

    But instead of just blocking the function key, how can I make it peform a different function?

  8. #8
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: VB6 Form and PDF

    Just add code to the function that fires when the key is pressed. In the article you linked, that would be in the IsHooked function.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  9. #9

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    14

    Re: VB6 Form and PDF

    I tried putting some function on it, it works. But if I put
    This Code:
    1. WebBrowser1.Navigate "some pdf file"

    It gives me an error message.
    See attached photo. Sorry but it's in Japanese. Maybe those error numbers mean something?

    They would translate to something like:

    'Navigate' Method Failed: 'IWebBrowser2' objects
    Attached Images Attached Images  

  10. #10

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    14

    Re: VB6 Form and PDF

    it's working now!!

    I just placed

    This Code:
    1. If WebBrowser1.Busy = False Then
    2. WebBrowser1.Navigate "some pdf file on the lan"
    3. End If


    Thanks to gigel_hifi and especially to jemidiah

  11. #11

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    14

    Re: VB6 Form and PDF

    Ooops now I have a new problem.

    The keyboard hook is working fine but when I press F1, it unloads the form containing the keyboard hook but it seems the F1 command is passed over to another form and closes it too.

    All forms exit through F1.

    Is there a way to clear the keyboard buffer used by the keyboard hook?

  12. #12
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: VB6 Form and PDF

    It depends. I could see your hook code firing on Key Down and your unload code firing on Key Up, which would cause the Key Down bit to close the hooked form and the Key Up bit to close the next form. If this is the problem, one solution would be to check the wParam argument in your key hook procedure and only do something if it's a Key Up event, or you could switch your form code to only respond on Key Down.

    It's difficult to tell without code what the problem is; this is just a reasonable guess. If this is the issue, there's no real way to prevent the Key Up from firing (well... without hooking it, which is silly).
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  13. #13

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    14

    Re: VB6 Form and PDF

    Say I have formA and formB. formB opens through formA. formB loads the keyboard hook on load. formB function keys work fine. When formB unloads or exits or deactivates, keyboard is unhooked. But when I press F1 to exit, the F1 is carried over to formA and exits that form also.

    By the way, formB.KeyPreview = false

    Is there a way to catch the F1 key before being passed over?

    Thanks. Ill look into the wParam argument

  14. #14

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    14

    Re: VB6 Form and PDF

    I tried
    Debug.Print wParam
    on Private Function KeyboardCallback.
    When I press key down on the other function keys, it prints 256 and then prints 257 on key up. But when I press on the F1 key, it only prints 256, I guess the key up event is carried over to the next form?

  15. #15

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    14

    Re: VB6 Form and PDF

    I have attached a sample code which replicates the problem. Although sometimes the problem isnt there, which is the same with the actual program.

    To replicate the issue:
    start the program
    on frmMain hit F2
    frmPDFViewer should show up
    on that form, try pressing the available functions keys on the keyboard, try alt+tabbing between the two forms many times and then hit F1 key on frmPDFViewer and sometimes frmMain should unload too.

    By the way, mouse clicks have no effect on the functions keys since the actual program doesnt use a mouse.
    Attached Files Attached Files

  16. #16
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: VB6 Form and PDF

    I'm sorry, I was only able to reproduce the issue once (the first time I tried it, even). That wasn't enough for me to figure out what could be causing it. My only instinct is to try a compiled version of the program and see if it has the same issue--perhaps it's just that the program is running in the IDE. When it closed both with the same key stroke, it had to propagate the KeyDown event to the second form, somehow, which is strange.

    If the issue keeps happening and you can't fix it, you could at least work around it. You could use a Timer and disable processing of key press input for a small amount of time after any window closes. It's not the cleanest solution, but it should work.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  17. #17

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    14

    Re: VB6 Form and PDF

    I tried running the compiled version, still has the same problem.

    How do I disable processing of key press input for a small amount of time? What command should I use for that?

    Sorry, I don't know vb6 that much

  18. #18
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: VB6 Form and PDF

    You could do something like define a public boolean "IgnoreKeys" in Form1 and add a Timer, say IgnoreKeysTimer, to Form1 which, when the Timer event fires, sets IgnoreKeys to False and disables itself. When Form2 closes, you could do Form1.IgnoreKeys = True and Form1.IgnoreKeysTimer.Enabled = True. You would set IgnoreKeysTimer's interval to, say, 250 (so a quarter of a second). With this setup, for a quarter of a second after Form2 closes, Form1 wouldn't process key input.

    Hopefully you get the general idea. If you need, I can edit the project you attached, though the changes are quite simple.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  19. #19

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    14

    Re: VB6 Form and PDF

    Thanks man, I'll try that.

  20. #20

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    14

    Re: VB6 Form and PDF

    I tried it, I still have the same problem. Not sure if I did it correctly though. Can you add it to the sample code I attached so I can check my mistakes?

  21. #21
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: VB6 Form and PDF

    See the attached project. If it doesn't work, try increasing timIgnoreKeys.Interval to something silly like 2000. If it still doesn't always prevent both from closing, the key down message is getting propagated early without you calling the next procedure in the chain, which is very strange.
    Attached Files Attached Files
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  22. #22

    Thread Starter
    New Member
    Join Date
    Feb 2011
    Posts
    14

    Re: VB6 Form and PDF

    WoW! Thanks man, as far as I can tell, it's working!
    I didnt have the code you added on frmMain on form_keydown.
    No wonder mine didnt work

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