how to disable system keyboard handling in Java AWT Application? - java

How can i disable such keys and their combinations as, for example, Alt ; Alt + F4 and others in my Java AWT application?
E.g. my KeyboardListener should handle that keys as 'usual' keys and combinations without closing window or entering window menu.

One way is to create a program in "kiosk mode", something that requires more than Java to achieve (such as JNA or JNI). If you google this or search this site for this, you'll find out more about it. If I were using your code, though, I'd be very frustrated and perhaps angry, unless this were being run on a dedicated kiosk terminal.
Edit: Another option is as per this thread: java-full-screen-program-swing-tab-alt-f4:
window.setExtendedState(Frame.MAXIMIZED_BOTH); //maximise window
window.setUndecorated(true); //remove decorations e.g. x in top right
window.setAlwaysOnTop(true);
Edit 2: and this brute-force method: Remove the possibility of using Alt-F4 and Alt-TAB in Java GUI

Found this solution:
for Tab - use Frame.setFocusTraversalKeysEnabled(false);
for Alt - add keyEvent.consume(); at the end of each key
event handling code block
Then, to find out if Alt or Ctrl key is pressed - use keyEvent.isAltDown() and keyEvent.isControlDown() methods of keyPressed or keyReleased events.
Thanks, #Hovercraft , for your quick response!

Related

Java AWT Robot - how to read item text / label?

I need to select appropriate option from the popup window. Unfortunately, it is not the popup displayed by the web page, so I cannot use Selenium framework for that purpose.
As I checked, it is possible to navigate to different option by pressing arrow keys. Thus keyPress and keyRelease​ from Java AWT Robot should work for me perfectly to select option and confirm the selection by pressing Enter key.
Unfortunately, I do not see a method to read currently selected item text. Without that I cannot find appropriate option. Is it possible to read item label using Java AWT Robot?
If the target application is another java application, then you should be able to get a reference to the component hierarchy and traverse it until you find the text field & label in question.
Assuming it's not a java application, then I don't believe it's possible to do this - at least directly. Robot just provides mouse / keyboard input, but you could use it combined with other classes in the toolkit to at least partially solve your problem.
Use the Robot methods keyPress & keyRelease to input CTRL-A. Then CTRL-C.
Once the text is in the clipboard, you could read it using Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor)
You might be able to use the same approach for a text label, assuming it is selectable.
This question is talking about using java.awt.Robot to alter text in another program (MS excel), but it might provide you some additional ideas for tackling the problem:
How can I select text in another application with java.awt.robot (example: shift+home)

Is there any possibility to put some process on top/activate the window by using java?

I'd like to set some process on top let's imagine for example it's web browser and in some condition it becomes on top - the window activates and I can send there key strokes and some mouse actions.
I've tried to find some in documentation or in web, found only one solution but it's not filling to my problem - Alt + Tab is required there.

JavaFX: How can I get the character the represents the platform specific SHORTCUT key code?

I am writing a JavaFX application and I have used accelerators to add shortcut keys to the application menus.
Now I am creating the application help and I want to describe the usage of the shortcut keys.
The recommendation for JavaFX accelerators is to use SHORTCUT instead of CONTROL (Windows) and COMMAND (Apple). This works fine and in the menus when running the application on different platforms show the right key combination.
For example, MenuItem Exit I have added the accelerator SHORTCUT_DOWN + X which is displayed as
Ctrl+X under Windows
and
⌘+X under Mac OS
Now I would like to get the explanations (Ctrl+X, ⌘+X) from the system in order to add it to the user help.
Is it possible to ask JavaFX for the presentation of the accelerator in the menu? Or get the presentation of SHORTCUT_DOWN used in the menus?
Thx in advance
Thorsten
According to the documentation, the getDisplayText() method
Returns a string representation of this KeyCombination that is suitable for display in a user interface (for example, beside a menu item).
So all you need is
String acceleratorAsString = menuItem.getAccelerator().getDisplayText();

InputEvent modifiers key

When I try to get (isAltGraphDown) if the ALT GR key is pressed when I click a button Java returns always false also if it's pressed.
Instead with e.getModifiersExText(e.getModifiersEx()) it returns Ctrl+Alt !!!
What's the problem?
and If I wanted to know if a user pressed a right Shift key?
When I was on the jEdit mailing list I saw a lot of these types of issues, usually when using non-US keyboard layouts. Occasionally other software on the system can also cause trouble, but most often it's a JVM bug. What keyboard layout are you using?

Why is my keyboard messed up in Eclipse?

I am trying to type in a pair of angle brackets in Eclipse, like "<>". However it shows up as a single quotation and a dot, like "'.". I tried a couple of times and found out that the angle bracket is actually located at back-slash's position.
Why is this happening? How can I change it back?
Thanks in advance.
Xi
I think this has to do with an annoying feature of Windows - I've had something similar for a long time in different programs (not just Eclipse).
If you have a non-English version of Windows, you probably have different keyboard layouts installed (for example, I have a Dutch and a US English layout installed by default). The key combination Alt + Shift switches between keyboard layouts. It's very annoying and it's a really strange feature - as if the keys on someone's keyboard would suddenly change?!
You can go into Control Panel, Keyboard Settings and remove the layouts that you don't want to use. (I removed the Dutch layout so there's only US English left), so that you can't switch to the wrong layout by accident again. Or you can go to the Advanced Key Settings in Keyboard Setting and turn off the key combination.
Hit Alt + Shift. If you don't want to spend time removing keyboard layouts like Jesper suggested, just hit Alt + Shift until you get the layout you need. It usually doesn't take more than a couple times. :)
I Am Doing This Via Windows Control Panel.
First time when changing font.I am restart Eclipse.(No solution than that to save my time) and then do this.
In windows 7 the language bar uses the LEFT ALT + SHIFT key sequence to change languages, so you can quickly change the language say to Spanish when writing a mail.
This sequence conflicts with many Eclipse shortcuts that use ALT + SHIFT, like rename!
You can remove the OS's setting by control panel > keyboards and languages > change keyboards > advanced key settings.
Julio
... Maybe you have your keyboard set for a different Locale(country)?
Also, you would probably be better asking this on SuperUser
Actually i think this must be something with Java SDK, because im having the same problem with IntelliJ. For example, in
public class MainActivity extends Activity {
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView iv
}
}
when i try to complete this snippet, with
ImageView iv = (ImageView) findViewById(R.id.iv_imagem);
after "iv" the character set in my keyboard is screwed, and instead of a QWERTY keyboard (i have portuguese language defined, and dont have other keyboards in control pannel) i have a AZERTY
in windows, other applications are is still normal
I had the same problem. I was editing Java code in Eclipse and the keyboard layout suddenly changed. On my windows taskbar I have a keyboard icon and I used it to change the keyboard layout between United States-International and Dutch. That did not solve my problem. My problem was solved when I had restarted Eclipse.
Keyboard handling of Eclipse is not perfect indeed: it confuses ctrl-alt with "altgr"!
So for example with Hungarian keyboard layout you cannot enter character '&', as it requires pressing altgr-C - but ctrl-alt-C was set to "SVN commit" by default...
(of course no problem in un-binding that combination in Eclipe, but it was not straightforward that ctr-alt conflicts with altgr, so what to look for)
I guess this probably happens if your script in Eclipse uses Robot class or any statements that use the combination of the keys, ALT+SHIFT OR CTRL+SHIFT. These combinations mess up with the functions of other keys after your script execution is completed or if it is stopped in between.
If you want to confirm if any of these combination is really messing up with your keys, then in your testng.xml file, just mouseover on any class name and it will be highlighted and underlined automatically, even though we have not pressed ALT nor SHIFT at this moment. It means that these keys are not released yet even though your script might have released them.
If you face this problem, then the control being in Eclipse, press CTRL+SHIFT and press ALT+SHIFT, and this resolves the above problem. Now, the keys in your keyboard work as usual.
with me, the strange thing is outside of eclipse (i use the ADT package), everything is ok. And restarting workspace, fixes the problem
this happens when i type any expression that might not be valid, like when i try to write
taskNameEditText = (EditText)findVie
of course, i want to write
taskNameEditText = (EditText)findViewById(R.id.task_name);
but after the first snippet, it changes the keyboard. And the code completion stops working too. So far the only working solutions i found where: 1 - restarting eclipse, 2 - start using IntelliJ
changing the key bindings wont work, just because i already have those unset. Also, "Alt + Shift" wont work, because this is not a windows (i use 7, 64 bits) problem

Categories

Resources