CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Join Date
    Jun 2006
    Posts
    109

    Problem with SendKeys ... please help!

    Hi!

    I'm trying to write a simple tool for my personal Use. But I'm facing trouble in making it to work as desired.

    What I'm trying to do is: When the program is started, it will register Ctrl + Alt + Shift + F12 as Hotkey. Application minimizes itself to System tray. In this state, when you select some text in another application (such as MS Word), I want this application to grab the selected text into Clipboard and then create a new Text file & write this Clipboard text to it.

    The Problem is: SendKeys "^c" works only when the Hotkey is pressed for the very first time. After that, SendKeys does not produce desired result. I added Debug.Print in Hotkey Pressed even and noticed that Hotkey is pressed correctly; so there is no problem with RegisterHotkey. The problem I see is with SendKeys.

    I request you to please download the Project from following URL:
    http://www.orbitfiles.com/download/id1642485529

    I also tried GetForegroundWindow and SetForegroundWindow to make sure that this program itself does not gain focus. (project files on above link does not contain these API test code.) I tried adding a Timer control with Interval = 20 and declared a Long var. I collected Window's Handle in this var on TimerControl_Timer event; later used the same with SetForegroundWindow before SendKeys. But even this method could not fetch the desired result.

    Actually I always faced this problem and never could really found a work-around for this. So, it looks like there is some error in my method itself and so I decided better to talk experts like you all and get my techniques corrected!

    Please let me know where I'm going wrong. Why SendKeys works only for the very first time when Hotkey is pressed? Why it stops working after that? Any solution for this?

    Waiting for your valuable reply ...

    Regards,

    Ruturaaj.

  2. #2
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,900

    Re: Problem with SendKeys ... please help!

    You are not doing anything wrong

    However, Microsoft have seen fit to block simple sendkey functions because they want to. (You can't even send ^C - you have to key it)

    Try using your application within a VB program - No problems

    Try using your program with NotePad - No problems

    I ALSO have this problem as I have developed program which allows text case to be reversed (because you forgot to push the caps lock key before typing)

    The problem lies in MS Word specifically - perhaps you are better to make a post to a site which specialises in MS Word quirks (and please - please report your findings back here if you get a good answer)

    You are probably doing
    1) Highlight the area of text
    2)Press the Hotkey combination
    3)Reverse case, whatever
    4)Replace the text back where it came from
    The idea being that all you have to do is hit ONE KEY at step 2

    Another solution could be

    1) Highlight the area of text
    2)Press ^C on the keyboard
    3)Press you Hotkey (grab the data from the clipboard - not from MSWord)
    4)Reverse case, whatever
    5)Move the new text back to the clipboard
    6)Press ^V on the keyboard to replace the text

    So the operator does


    Highlight Text
    ^C
    Hotkey press
    ^V

    Bit messy but after all, you are trying to work with MS Word

    I am sure that someone has the ability to come up with a solution - this is the ultimate application which solves the problem which affects millions of people very second of every day.

    eg, The brilliance of the thousands of gurus at Microsoft has built this type of function into PARTS OF MS Word, but they think that everyone in the world just uses only word and one would never need this fundamental basic function in other programs such as "Code Guru Forums"

    ie, THIS FUNCTION SHOULD BE AVAILABLE IN THE OPERATING SYSTEM !


    Have a look at my feeble attempt to create the Hotkey Wonder

    Once the program is launched the idea is to simply highlight text - then press F12 to give you Propercase or F11 to give you reversed case (I PLAYED AROUND WITH COMPLEX HOTKEY COMBINATIONS BUT THEY MAKE NO DIFFERENCE WITHIN WORD)

    InCase.exe

    You need this program In Case you make a mistake typing the CASE of words - so its called "InCase"

    Cheers (Happy hunting for hoykeys !)

    Now if Jonny Poet got hold of this problem in his teeth I am sure the problem would be resolved !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    (I am happy to post my source code but I don't want all the gurus to fall on the floor laughing)
    Attached Files Attached Files

  3. #3
    Join Date
    Jun 2006
    Posts
    109

    Re: Problem with SendKeys ... please help!

    Hi! Thanks for your reply. Actually I would love to make my tool work in one stroke and yes, I can always make the application to have multiple key strokes. But then this will be another "escape" that I always use to find when I face this trouble! (and I have done it so many times! but now I'm really eager to find some proper solution for this!). I understand your suggestion will work. But there are so many applications (such as Dictionary.com's Desktop application) which work on the method that I'm trying to do.

    These applications, on one key stroke, collects selected text from "any" editor application (which allows the copy selected text functionality; security applied PDF files are beyond the scope for sure). So, actually whatever I'm trying to do is nothing new or special ... it's just the thing that I'm not able to get it to work correctly. I tried keyb_event to simulate the Ctrl + C; but it didn't work for me at all!

    However, I tried your application ... and it worked as many times as I pressed the Hotkey! How you did it man?! can you please share your source code with me? or at least the method you're using to collect selected text from another application on Hotkey press event? Are you using any other method of Register Hotkey and then grab the Hotkey_Press event than what I have implemented in my code?

    Eagerly waiting for your reply ... and I'm sure the Gurus here will not laugh at your code; because your code is surely gonna be something to learn from ... nevertheless it's doing what it says! So, please share your code (or at least thoughts with me. If you don't want to share your code here on Forum then you may send that to me directly on my email address: mailme_friends@yahoo.com)

    Thanks in advance!

    -- Ruturaaj.

  4. #4
    Join Date
    Jun 2006
    Posts
    109

    Re: Problem with SendKeys ... please help!

    Just one thing to mention ... I could not test your tool with MS Word because MS Word has F12 assigned for Save As (at least in my installation of MS Word 2000). In fact every function key has something to do in MS Word! But, I hope your tool works fine with MS Word as well. :-)

  5. #5
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,900

    Re: Problem with SendKeys ... please help!

    Thats the point - its the ^C which gets intercepted by MS Word so the Hot key assigned doesn't help

    We need a way of achieving ^C and ^V programmatically so that MS Word does not intercept these commands

  6. #6
    Join Date
    Jun 2006
    Posts
    109

    Re: Problem with SendKeys ... please help!

    Hi George; well, in my application ^C is nowhere in the Hotkey key sequence. So, there shouldn't be an issue of Word (or any other application) intercepting it. The Hotkey is Ctrl + Shift + Alt + F12. And when this key is pressed for the very first time, the SendKeys "^C" works correctly. However, after that, something goes wrong and the SendKeys stops working. It's not with just MS Word, I noticed the same with NotePad, too! That's the area where I'm not finding any solution ... any help?

    ---

    *P.S.: To see what I'm talking about, please open my project and minimize it to system tray. Later, open Notepad with some text file, select some text, hit the Hot key sequence, you will notice that application running in system tray has collected the selected text from Notepad and created a text file in application folder itself. Now, select some other text and try to do the same thing ... you will notice that Text file is blank now. This is because I have set Clipboard.Clear on Hotkey event before sending ^C key. But it's the SendKeys that does not work correctly and hence Clipboard is empty and so the file created after that!

  7. #7
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Problem with SendKeys ... please help!

    Hi, rutu, I tried your application and I'm getting ALWAYS an empty file. It seems to contain only a CRLF sequence for some reason. It never worked the first place. What am I to do to get it working once?

  8. #8
    Join Date
    Jun 2006
    Posts
    109

    Re: Problem with SendKeys ... please help!

    Hi WoF;

    Thanks for your interest. Please follow these steps:

    [1] Compile VB6 project into Executable file.
    [2] Run the Application.
    [3] Close application from Title bar close button (i.e. the regular [X] button on title bar). This will minimize application to system tray.
    [4] Open Notepad and open any Text file (or write something) in Notepad.
    [5] Select some text in Notepad.
    [6] Press Ctrl + Alt + Shift + F12.

    If everything goes fine then you will see the text you selected in Text file. Else, you can conclude that code is not working! :-(

    Please go through the source code and let me know why SendKeys fails when called from Hotkey. Your thoughts will surely help me to learn something new ... waiting for your reply!

    -- Ruturaaj.

  9. #9
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Problem with SendKeys ... please help!

    Ok. I played around a little.
    I'm not sure what happens, but do the following:
    1. Open Notepad with a piece of text.
    2. Mark some text
    3. Press Shift-Ctrl-Alt-F12
    Textfile will contain the marked text.

    4. Click on your program icon in the systray
    5. Mark some different text
    6. Press Shift-Ctrl-Alt-F12
    Textfile will contain new text. Program works as expected.

    If you don't click on the systray icon in between, the textfile will be empty next time you press the hotkey.
    I don't know why this is. What happens, when the user clicks on the systray icon?
    I know it is not a direct help, but maybe this observation leads us further in discovering the problem...

  10. #10
    Join Date
    Jun 2006
    Posts
    109

    Re: Problem with SendKeys ... please help!

    Hi WoF!

    This is a nice observation indeed and something that I did not notice before! Well, we may say that click on system tray icon means application getting focus for a while. Does it require to happen that?!?! In case of keyb_event API, we explicitly need to pass "key release" message to simulate Key Up event. I don't see any analogy of this sequence with our application, however it looks like SendKeys needs to shift the focus. Hmm, I will see to it and will get back here with my findings ...

    Thanks so much for your time!

    Regards,

    Ruturaaj.

  11. #11
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Problem with SendKeys ... please help!

    Quote Originally Posted by George1111
    Now if Jonny Poet got hold of this problem in his teeth I am sure the problem would be resolved !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    Sorry friends, I looked at it, its really nice code, cannot see any problem with it. It seems to be like WOF said something which has to do with needing to be clicked again to foreground. But I couldn't find out why this is.

    (I am happy to post my source code but I don't want all the gurus to fall on the floor laughing)
    Dont invalidate yourself this code is nice.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  12. #12
    Join Date
    Jun 2006
    Posts
    109

    Re: Problem with SendKeys ... please help!

    Yes, even I'm also not finding any solution for the problem! I tried following things but could not get it done correctly ...

    [1] Instead of setting startup form from Project Properties, I made it Sub Main. Also, instead of using form's Show method, I used ShowWindow API with SW_SHOWNOACTIVE flag. Form shows correctly; but the problem remains.

    [2] Instead of registering the Hotkey on form, I transformed the Hotkey code completely into Module where the Callback as well as Register Hotkey, both are registered in Module instead of VB Form. I was thing that when the Hotkey is pressed, it might be the case that control is transfered to the Form behind which the Hotkey is Registered. No logic for this really because the Hotkey handling code was still in the Module. But just to remove this function call completely from Form to pure code module, I tried this way. Hotkey worked but the problem still remains. As WOF said, it requires at least one click on System Tray Icon to get it to work correctly again.

    Still to try ...

    Currently I'm using vbAccelerator's System Tray Form template module for System Tray functionality. Now, thinking of moving System Tray related code to module. If this still fails then instead of attaching the Form's Icon as System Tray icon, I will attempt to create StdPicture object by loading the Icon either from Resource or directly from external file. This is the final step that will make it to work completely independent of VB Form (and I hope, I will get the final working solution here!).

    In the mean time, if you get any other idea of solution for this problem then please let me know. Now after spending so much of time on this issue, I'm really curious to see how this can be made to work properly. As I said, there are so many applications that already have this sort of functionality implemented (mostly Text to Speech tools which read aloud the selected text in any editable application window). So it's just me that finding no solution for this issue.

    -- Ruturaaj.

  13. #13
    Join Date
    Jun 2006
    Posts
    109

    Re: Problem with SendKeys ... please help!

    Quote Originally Posted by JonnyPoet
    I looked at it, its really nice code.
    Thanks for the appreciation Jonny! Please let me know if you get any solution or at least some idea to get this one to work properly ...

    -- Ruturaaj.

  14. #14
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Problem with SendKeys ... please help!

    Quote Originally Posted by rutu_programmer
    Yes, even I'm also not finding any solution for the problem! I tried following things but could not get it done correctly ...

    ....As I said, there are so many applications that already have this sort of functionality implemented (mostly Text to Speech tools which read aloud the selected text in any editable application window). So it's just me that finding no solution for this issue.
    -- Ruturaaj.
    No you are not the only one, we together are obviously not fining the correct way. My point is, I cannot understand why it works when the icon is clickd again, because I cannot see in my mind what this is changing for the clipboard. As the clipboard should always copy from the selected text and i'm sure it does, and the Hotkey also works because it obviously creates a neew, but empty, file we see the loop is running. So there are two possibilities: a) sendkey doesn't work

    b)the clipboardpointer doesn't point to the clipboard

    But as it obviuosly is cleared by the clear method it all points to the direction that sendkeys doesn't work or goes to a wrong application ?
    But why it then works correct when we click to the icon again ?
    Do you know a way to imitate this click on the icon ? Maybe this would be a workaround ?
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  15. #15
    Join Date
    Jul 2007
    Posts
    6

    Re: Problem with SendKeys ... please help!

    ahh yes i was looking for this thread too, thanks a ton

Page 1 of 2 12 LastLast

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

Featured