Good afternoon, I want to make the display of the active language of the keyboard layout in my application, simply because in full-screen mode it is not convenient to control it every time you exit it. If you know a good solution, please share it)

  • if you have a bad solution, show me, otherwise I didn’t find any at all. Shows the locale, but not the layout - Victor
  • @Viktor Bad too, I highlighted a good not because I know a bad solution, but because I would rather prefer the absence of this function than its presence in the form of a crutch. - ProstoCoder

1 answer 1

To catch the layout switching event in the system without using native libraries will fail. However, you can get the Locale from the InputContext of the component or window you need. By periodically polling the context, you can update the mappings within your application. Swing example:

import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Main { private static void createAndShowGUI() { final JFrame frame = new JFrame(""); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JLabel label = new JLabel(frame.getInputContext().getLocale().toString()); frame.getContentPane().add(label); Timer timer = new Timer(2000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { label.setText(frame.getInputContext().getLocale().toString()); } }); timer.start(); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } } 
  • Thank you very much! - ProstoCoder