Click here to Skip to main content
15,884,062 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I can come up with sample code if needed, but haven't done that yet.

In summary though I'm very confused how my setting up a global hotkey in C# / .NET code (using pinvoke win32 calls) that when triggered opens a form that has just a webbrowser control + calls (in forms _Load() method):

this.BringToFront();
this.Activate();
this.Focus();
webBrowser1.Focus();
this.ActiveControl = webBrowser1;
webBrowser1.Document.InvokeScript("document.getElementsByTagName('div')[0].focus();");

leads to the form being shown + responding to key events from jQuery keydown handler, yet the jQuery keypress handler doesn't work until I click the browser control with the mouse usually.

For now I handled this via using the ProcessCmdKey() overloaded method for the form to call a web browser js method and inject typed text that way, but it's ugly like with how it's hard to detect the character for keys like -, !, @, ... (currently they just don't work)

I even tried in javascript doing window.addEventListener("keypress",...

and still that event doesn't come until I click the web browser control typically unless I manually click it.

It's not every time though. I've noticed that it seems like if I had my desktop active like right after clicking a desktop icon then doing hotkey sequence, then the keypress event works right. But it seems if I had any other program active when the hotkey sequence happened, then the popup form comes up and only the keydown event works.

Very confusing.

Any ideas how to fix so javascript keypress event can be used?

Thanks!

What I have tried:

Started with:

this.Focus();
webBrowser1.Focus();

working up to:

this.BringToFront();
this.Activate();
this.Focus();
webBrowser1.Focus();
this.ActiveControl = webBrowser1;
webBrowser1.Document.InvokeScript("document.getElementsByTagName('div')[0].focus();");


Also started with jQuery 3.1.1 $(document).keypress() handler and worked my way lower to window.addEventListener("keypress", function(e){...

I implemented some pinvoke code to simulate a mouse click, but it doesn't seem to work right unless I literally move the position of the cursor and that's very awkward appearing for a published product.

For now using the forms ProcessCmdKey() overloaded function is handling text key input, spacebar, and backspace, but it's awkward trying to handle more keys like the shift version of numbers (!,@,#,$,%,^....)
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900