Preventing SWT Browser Elements from Receiving Mouse Clicks - java

I'm working with the eclipse SWT toolkit, and I'm trying to use it to create a browser window that only passes mouse clicks to the underlying document conditionally (I want to stop Flash and Javascript in the page from getting clicks). I'd like some way of doing one of:
Examining mouse events as they come in, and only passing them on to other listeners based on conditions I specify.
Removing all listeners from a window, and only putting back the ones I want.
Are either of these possible?

Browser, like other SWT components, have addMouseListener method. So you could implement your own listener a pass only which one you want.
see javadoc of browser
Edit
According to your request, there could be two possible ways to do it.
First, you could use listening of events from JavaScript in browser (there is no way to avoid JavaScript if you work with html pages). If you know that you will have Mozilla browser render core (you have to install XUL Runner), you could use JavaXPCOM, but that's big unknown for me.
snippet - listen for DOM mousedown events with javascript
Second, you can call Java functions from JavaScript (again, handle onclick event, and then decide on Java, if you don't want to use JavaScript for it).
snippet - call Java from JavaScript
But frankly both ways are more ugly-er that proposed way by pure JavaScript.

Related

Listener for when the user changes window (not necessarily windows created by the application)

I need to add a functionality in my application that would require me to know when the user changes window (it could be a browser window, my application's window or any other window).
Ideally, it should be possible for me to print the window's title when it gets focus. The problem I'm having finding a solution to this problem is that I only get links that tell me how to add a focus listener on windows I'm creating, which I already know how to do and doesn't help me in the slightest.
The solution should at least work on Windows 7.
The (major) problem you face is that Java filters system events so that you can only recieve events that are related to you. AFAIK this is all done at a native level, so there's no way to intercept or modify this filtering process.
The only solution is to create another "event loop" using JNI/JNA which will allow you to intercept the event messages being passed about the system and handle them the way you want to.
While slightly more complicated, it does open up a world of opportunities...

How to send key-ins to certain windows

I'm looking to write a relatively simple key macro for my own edification and also for my own use.
I intend for it to be able to run in the background when I run other programs, and at the press of a button will enter certain text into the active window. To be more specific, I want to go in a video game and use it to automatically send messages instead of having to type them.
I Googled around a bit, but apparently I'm not using the right keywords because I'm not really sure where to start. The closest I came was finding the Keystroke class, but that appears to be used for receiving keystrokes, not sending them.
I would appreciate and info regarding, or directing me to a resource for, these issues (how to send keystrokes to windows and anything about targeting which window etc).
To send key strokes you can use java.awt.Robot
To Choose which window to activate you can look around for ws script (windows script) or old VB6 code and use it to make VB script (simple text file of extn .vbs)
Or you can junk all that and use http://www.autohotkey.com/ which has window activation, sending keys, doing things on press of certain keys (like Windows Key + B) or macros.
To get a window to activate I had made an exe long back, but no longer use it, can get it from http://sourceforge.net/projects/win-utils/files/Window-Position/rel%2001/ (but only get this if the others do not work as need to seperately get COMCTL32.ocx and install that
If you do not want to use autohotkey you can use Jini to call platform specific functions, with a wrapper to call correst OSes functions. Never done it my self, when i had to use it i would make a process to call a exe that made the window come the front.

windowless webbrowser

I need some kind of webbrowser backend (don't know if it's proper name for such thing). Generally I need high abstraction of html page with controls, ability to create events (button push or selecting an item from combobox), javascript interpreter, etc.
Are there anything of that kind?
I think you want something like HtmlUnit. From the page:
HtmlUnit is a "GUI-Less browser for Java programs". It models HTML documents and provides an API that allows you to invoke pages, fill out forms, click links, etc. just like you do in your "normal" browser.
It has fairly good JavaScript support (which is constantly improving) and is able to work even with quite complex AJAX libraries, simulating either Firefox or Internet Explorer depending on the configuration you want to use.
You might also want to check out Selenium which lets you control a real browser programmatically.

How can I override the browser keyboard shortcuts in my GWT/GXT app?

I'm using GWT and GXT and want to use keyboard shortcuts like CTRL-S (or CTRL-SHIFT-S) to save the current form. Problem is, I can't figure out how to override the browsers built-in CTRL-S. I tried using a componentEvent and calling stopevent, cancelbubble, etc with no luck. I also tried using
Event.addNativePreviewHandler(new Event.NativePreviewHandler(){
}
and calling event.getNativeEvent().preventDefault(); to try and stop the event but that doesn't work either.
I'm at the point where i can get my keyboard shortcuts to work, but they also fire the browser's event too (like opens a browser save dialog)
Anyone have an idea/hint on how to get this to work? I actually have a number of shortcuts I want to use but many conflict with the browser - just trying to figure out how to override the browser's actions.
Its better to find key combinations rather than trying to override the keyboard shortcuts which is not a standard approach.
Call the preventDefault() method on the KeyDownEvent. This works when I added a Dom Handler and also referenced from the javascript forums of how gmail does it - i have noticed that while CTRL-S does work, CTRL-N does not work in Chrome. Will be testing other browsers later.
Hope this helps,
Ian.
To answer my own question - we opted to go with CTRL+ALT shortcuts which don't conflict with any built-in browser keyboard shortcuts.

Block all other input to an application and control it from a wrapper in Java

I have a windows application which has a complex GUI that I would like to hide from users. In order to do this, I would like to create a wrapper with an extremely simple interface that overlays this application and automates a number of actions when a user clicks a single button on the wrapper. (I hope "wrapper" is the proper term.) Is it possible to use Java to block input to the underlying application so that users cannot inadvertently mess up the automation? How would I go about this? Also, how can I automate key presses and clicks to the application without hijacking the mouse? Is this possible in Java?
I have looked at java.awt.Robot, but it appears to hijack the mouse.
I have also looked at AutoIT, but it too hijacks the mouse and does not integrate with Java.
Neither of these options seem powerful enough for what I need, but I do not know how else to proceed.
I recommend that automation via the GUI only as the last resort if you really have no other alternative.
If there is an API that your application exposes, I would try to use that. For example, if the GUI is implemented in one DLL and the logic in another, then you can use JNA to load your application logic DLL and invoke the application functions directly from java. Even better would be if your application exposes a COM/OLE interface - there are plenty of Java<>COM briges, that will alow you to call this interface directly, e.g. Jacob.
If you really have no choice but to automate via the GUI, then here's how to go about doing that:
Use JNA to access the windows shell API. You can then use ShellExecute to launch your wrapped application. Specifically, passing SW_HIDE as the window mode should help ensure that the application does not appear.
Use JNA to access the windows API FindWindow to find your application window. You can also make it invisible using the ShowWindow API, just in case step 1 did not work (not all applications are written to use the nCmdShow parameter.)
You can now post messages to the application window using PostMessage. You can send keystrokes and mouse events using windows messages. E.g. See WM_KEYUP, WM_LBUTTONDOWN.
Because the wrapped application window is made invisible, you don't need to "block" that application, The user simply cannot access it's GUI. But you can still programmatically send input to it.

Categories

Resources