Jump to content

Window.postMessage not working for Java App (c#)

I'm working on coding an app in C# to send keypresses to applications that are not in focus. I initially had it working flawlessly with internet explorer where I sent "F5" every 5 seconds using postMessage, and this worked regardless of whether the window was in focus. I then attempted to do the same with Minecraft, written in Java, but it ignored the input (F5 should have toggled third person mode). What confuses me is that I know I have the correct handle, as I used proc.MainWindowTitle and it returned the correct title. My guess is that Minecraft, or perhaps all Java apps, do not process input the same way other apps do. How could I make a Java application such as Minecraft accept keyboard/mouse input without it being in focus?

 

Code snippet:

[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
        
 while (true)
            {

                Process[] processes = Process.GetProcessesByName("iexplore");

                foreach (Process proc in processes)
                    PostMessage(proc.MainWindowHandle, WM_KEYDOWN, VK_F5, 0);

                Thread.Sleep(5000);
            }

And yes, I know that I have no way of breaking the loop currently, this is just to test the basic functionality first.

PC Specs:

CPU: AMD 1700x Cooler: Corsair H100i V2 Motherboard: Asus Crosshair VI Hero RAM: 4 * 8GB G.Skill RGB DDR4 Graphics: EVGA GTX 1080 Ti SC2 Storage: Samsung 960 EVO 500GB Case: Fractal Design Meshify C PSU: EVGA 750w G3 Monitors: Dell SG2716DG +  2x Dell U2515H

 

Freenas specs:

CPU: Intel Xeon E5-2650 V2 Cooler: Some noctua cooler Motherboard: Supermicro X9 SRL-F RAM: 8 * 8GB Samsung DDR3 ECC Storage: 6 * 4TB Seagate 7200 RPM RAIDZ2 Controller: LSI H220 Case: Phanteks Enthoo Pro PSU: EVGA 650w G3

 

Phone: iPhone 6S 32 GB Space Grey

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, ThatFlashCat said:

I'm working on coding an app in C# to send keypresses to applications that are not in focus. I initially had it working flawlessly with internet explorer where I sent "F5" every 5 seconds using postMessage, and this worked regardless of whether the window was in focus. I then attempted to do the same with Minecraft, written in Java, but it ignored the input (F5 should have toggled third person mode). What confuses me is that I know I have the correct handle, as I used proc.MainWindowTitle and it returned the correct title. My guess is that Minecraft, or perhaps all Java apps, do not process input the same way other apps do. How could I make a Java application such as Minecraft accept keyboard/mouse input without it being in focus?

 

Code snippet:


[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
        
 while (true)
            {

                Process[] processes = Process.GetProcessesByName("iexplore");

                foreach (Process proc in processes)
                    PostMessage(proc.MainWindowHandle, WM_KEYDOWN, VK_F5, 0);

                Thread.Sleep(5000);
            }

And yes, I know that I have no way of breaking the loop currently, this is just to test the basic functionality first.

And application is usually not just 1 window (handle), but "many" (controls etc have their own handle).

 

Sending it to the main hwnd (top level window in hierarchy, of which btw there can be more than one) it will depend on the application / hwnd handler if it propagates input messages to descendants or not. Likely its "eating" it without propagation in this case.

 

Its unrelated to java as regardless of language or frameworks used, in order to be visible it will need one ore more o.s. ui components (on windows this would be a window). During gameplay though, input might be retrieved through dx apis an not neccesarily the hwnd way. It might depend on the implementation then if hwnd input reaches that same path (in dx input).

 

Link to comment
Share on other sites

Link to post
Share on other sites

45 minutes ago, Bartholomew said:

And application is usually not just 1 window (handle), but "many" (controls etc have their own handle).

 

Sending it to the main hwnd (top level window in hierarchy, of which btw there can be more than one) it will depend on the application / hwnd handler if it propagates input messages to descendants or not. Likely its "eating" it without propagation in this case.

 

Its unrelated to java as regardless of language or frameworks used, in order to be visible it will need one ore more o.s. ui components (on windows this would be a window). During gameplay though, input might be retrieved through dx apis an not neccesarily the hwnd way. It might depend on the implementation then if hwnd input reaches that same path (in dx input).

 

Well, perhaps what I am trying to do isn't possible.

PC Specs:

CPU: AMD 1700x Cooler: Corsair H100i V2 Motherboard: Asus Crosshair VI Hero RAM: 4 * 8GB G.Skill RGB DDR4 Graphics: EVGA GTX 1080 Ti SC2 Storage: Samsung 960 EVO 500GB Case: Fractal Design Meshify C PSU: EVGA 750w G3 Monitors: Dell SG2716DG +  2x Dell U2515H

 

Freenas specs:

CPU: Intel Xeon E5-2650 V2 Cooler: Some noctua cooler Motherboard: Supermicro X9 SRL-F RAM: 8 * 8GB Samsung DDR3 ECC Storage: 6 * 4TB Seagate 7200 RPM RAIDZ2 Controller: LSI H220 Case: Phanteks Enthoo Pro PSU: EVGA 650w G3

 

Phone: iPhone 6S 32 GB Space Grey

Link to comment
Share on other sites

Link to post
Share on other sites

20 hours ago, ThatFlashCat said:

Well, perhaps what I am trying to do isn't possible.

Send keys will work if the receiving application has global catch. Which nearly all OpenGL and DirectX don't have. These you need to specify the handle to send the key to. as mentioned you might have couple hundreds in a sample application. The hard limit by default is 15,000 if i recall but you will probably hit the max GDI object limit before that. If the app have a keypress event to capture a key then you NEED to have it focused as key press only trigger on focused controls.

 

Why it works with F5 on explorer it's because it's a global key. No matter what you have highlighted pressing F5 refresh the page. They did not individually coded key press event for each focusable control in hope someone press F5. They hook the whole keyboard in the app and catch those keys.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×