How to receive the keystrokes without having a text field focused? - java

In my Swing application I am using a bar-code scanner attached to the machine using USB port. I want to trigger an action (popup a window) as soon as some thing is scanned using bar-code scanner. I do not have any text field (e.g. JTextField) focused before the scanning is done.
But if I scan something now without any focus on text filed it is scanning and not registering the value to the application, just like how we press A,B,C,D in the keyboard without having the cursor focused on a text field region. I need to notify the application internally when the scanning happens and register the value from bar-code scanner in a text field.
How do I do this?

You can add KeyListener to any component including your window. So if at least something is in focus you will receive the events.
Moreover you can use AWTEventListener: Toolkit.getDefaultToolkit().addAWTEventListener(listener, eventMask). Play with eventMask to get events you are interesting in. This allows getting event on AWT level. Even if you have 10 separate windows in your application and want to catch events from all of them you can do it in one single place.

Related

(Java) Can I use KeyListener to input text? (inside event dispatching thread)

I'm building a Swing game that is drawn entirely (everything, menus included) in single JPanel object. I have a game loop in this JPanel, which handles everything from updating the state of the game to drawing it and so on.
Now, I need to somehow enable the player to type in their own text in some of the menus. Creating a new player profile, saving some other information and the like. I can't seem to find a good way for doing this. Using a Scanner is obviously out of the question, since doing that will interrupt the event dispatching thread, freezing the game on the spot (I learned that the hard way).
I have concluded that the only way to do this is to use the KeyListener to somehow record the keys I have pressed on the keyboard. I have been experimenting with its keyTyped method, but my results have been poor. I have been able to make it record my keypresses, but the problem is, it records EVERY key I have pressed, including backspace, control, TAB, and so on... Plus, every character that I type is outputted capitalized, regardless of whether I have Caps Lock on or not.
I hope you understand what I'm trying to achieve here. So my real question: Is there any easy way to record typing using KeyListener? Or is there some other way that can be used inside of the event dispatching thread?
Your options are:
Use a standard popup dialog and get your text that way
Embed a text field into your panel to receive input
The problem is that a KeyListener is just that, it listens for key events. The standard text input components combine this with a DocumentListener for processing text

Writing text into another application's text controls (on mouse focus)

I need some advice on how to write text into another application's line edit when that control receives mouse focus.
For example:
I opened google.com and clicked in the "Search" field (so the mouse focus is in "Search" field).
I need to programmatically write text in that field.
I have used Robot class but some symbols are not supported. Are there any ways to solve this problem?

SWT disable window causes lose focus

I'm making custom dialogs that I want to pop up and disable the main shell behind it so that it cannot be clicked while the dialog is active.
My initial plan was something like as follows:
shell.setEnabled(false);
doDialogStuff();
shell.setEnabled(true);
this worked but as I close the dialog, it loses focus of the shell that was open before the dialog. I managed to sort of fix it by adding
shell.setFocus();
after the last line but this is messy and causes the screen to flicker as the window loses and then gains focus in a split second, also, it sometimes doesn't regain focus and I can't understand why :/
Is there a better way to disable the background window without it losing focus.
Thanks in advance SO peeps
You should create a custom dialog based on this tutorial.
This way you just have to set the modality of the dialog to whatever you need exactly and the dialog will take care of the rest for you.
This should be helpful as well (Javadoc of Shell):
The modality of an instance may be specified using style bits. The modality style bits are used to determine whether input is blocked for other shells on the display. The PRIMARY_MODAL style allows an instance to block input to its parent. The APPLICATION_MODAL style allows an instance to block input to every other shell in the display. The SYSTEM_MODAL style allows an instance to block input to all shells, including shells belonging to different applications.
The proper thing to do is create the dialog as a modal window. When you create the dialog's shell you should do something like
dialogShell = new Shell(mainShell, PRIMARY_MODAL | DIALOG_TRIM);

Key press events for GXT FileUploadField

GXT 3.x only.
It is becoming apparent to me that Sencha had deliberately designed FileUploadField to shunt off all key press events from ever being detected.
I tried to intercept onBrowserEvent(Event) and could not detect any key press events which I would have generated by keypresses while having focus on the FileUploadField component.
Where is the key-press event shunt?
I could not find any keypress handler insertion methods.
I wish to allow triggering the file upload by either press on the space-bar or enter key.
Short of rewriting a whole new component from scratch, could someone advise me what I could do to achieve my goal of a keyboard activated file upload?
onBrowserEvent won't recieve any events unless you sink them - did you make sure to call sinkEvents? How are you adding handlers? If you use addDomHandler, it will sink them for you, but addHandler either assumes that they are not dom events, or that you already called sinkEvents. Without sinking an event, the browser doesn't know to pass that event on to a GWT widget. If all events were sunk automatically, then every time you moved the mouse across the page you would see a firestorm of events as mousemove fired for every widget you passed, and all of its parents.
If you override onBrowserEvent, then you are building the method that describes how to handle the actual event that comes from the browser - that is where the com.google.gwt.user.client.DOM class wires into the Widget to give it events. Short of making that method final, there is no way to prevent you, the widget user, from getting those events as long as the browser is generating them and passing them through the event listener.
Even if onBrowserEvent has been overridden and made final, you can still get access to many events by creating a NativePreviewHandler and checking where the event is occurring. This gets you in to the event before it even goes to the widget itself - there you can call NativePreviewEvent.cancel() to prevent it from happening on the widget itself, or you can handle it early in the handler.

regain focus after java.awt.Robot.keyPress()/mousePress()

I writing an application which controls another application by using the keyboard only. To more concrete, the application simulates key presses and mouse clicks when a certain key is pressed on the keyboard. For example, pressing on the 'x' key simulates a mouse click on the [X] in the rop right corner, followed by a little sleep of 2 seconds and an 'enter' to confirm the exit dialog. Pretty easy. I am developing this application in Java.
Sending a key press or a mouse click is very easy with java.awt.Robot. I am facing one little problem. Say I have configured a key which will click somewhere on the screen. The problem is that consecutive key presses aren't catched anymore, as my application lost its focus caused by the mouse click outside it's window.
My question now is: what is the best way to be sure that my main application keeps the focus? Is there a way to focus my application again after the key presses and mouse clicks are sent out? Is there a better way?
Thanks in advance.
If your application lost the focus. because you or your Robot clicked to somwhere else, the Robot must click on the application again before sending a new key. In c/c++ you could force the focus to the application (a non-trivial task), not in Java!
You might want to take a look at Component.requestFocus() to see if can do what you want.
Be aware however that window focusing has very platform dependent behaviour, so you will probably need to do quite a bit of testing to ensure that your code does what you want in all circumstances.
I managed a way to prevent applications from losing all focus in Java.
By placing a WindowFocusListener on the frame (or dialog) and calling setVisible(false) followed by setVisible(true) in windowLostFocus the component will re-appear as soon as it is dissapears (not the prettiest solution but it does work).
By then calling component.requestFocus() your robot should be able to continue where it left off

Categories

Resources