assign a function to a key on the keyboard [duplicate]

2

I am creating an application for a cattle auction where it has a lot, the animal's gender, whether it is the view or the term, and the value field of the lot where it increases, while the auctioneer speaks the bid.

Getting typed value manually loses a lot of time so I want to assign the "." key. to increase by 5 reais, the "+" key to increase 10 reais, the "-" key to increase 20 reais and the "*" key to increase 50 reais.

How do I assign this function to the keys.

Below is the screen code.

    
asked by anonymous 26.03.2015 / 21:22

1 answer

2

There are two basic ways to do this.

KeyListener in components

The first is to add KeyListener to your components to capture keyboard events, check the keystrokes, and take action as required.

However, this listener only works if the components have the focus, so it needs to be added to each component that can be focused when pressed.

See how to implement KeyListener in the official tutorial .

Global Listener

If you want a global listener in your program, you can use the KeyboardFocusManager class.

See the example I posted on another response .

    
26.03.2015 / 23:08