assign a function to a key on the keyboard

Asked

Viewed 963 times

2

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

Typing the value manually loses a lot of time so I want to assign the "key." to increase more 5 real, the key "+" to increase 10 real, the key "-" to increase 20 real and the key "*" to increase 50 real.

How I assign this function to the keys.

Below is the code of the screen.

1 answer

2

There are two basic ways to do this.

KeyListener in the components

The first is to add a KeyListener to its components to capture the keyboard events, check the keystrokes pressed and take the necessary action.

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

See how to implement a KeyListener in the official tutorial.

Global listener

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

See the example I published in another reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.