Keyboard Events

3 minutes
Share the link to this page
Copied
  Completed
KeyboardEvent objects describe a user interaction with the keyboard; each event describes a single interaction between the user and a key (or combination of a key with modifier keys) on the keyboard.

Transcript

Hello, in this Java programming video, we are going to take a look at events, more specifically keyboard events. And what we're gonna do, we're gonna get some input. I'm going to print out the input to the console, really simple. You could put it in so like the draw string, what we're doing here, I want you to have an exit task, try and figure out how to do that. If you have any question regarding if you need any help, feel free to reach out. Okay, so what we're going to need to do is include a or import module for Java, AWT.

Event, aspirants, so this is all the events and implement. So this was I was to detect keyboard inputs and what we need to do in the in Yeah, that's fine. We got some metadata on initialize. We're gonna you know, add millisecond. So in the init method, we need to do add key listener, and we need to pass in. So it's just this class, what is basically listening on.

So that allows you to actually listen to them. And now we need the methods that this is one event. So we just did. He prepped for this even if he had been pressed crazy on the key event events and let's duplicate this method. The other ones are very similar. The essence is key released, so when a key has been removed, and this key type as well.

So key. typed does deal with it in here but if you want to know when key has been pressed or released, you can do that when you do regular typing. You can do it in here. So we'll do trawl. And I'll call it ch equals event. dot get key Cha, so you're getting the counter.

And now, we can just log in at the console, we can do what's called the dot, dot print ln. And this is going to print out a chart that we want to see what we get. I'm pressing key. And Oh, there you go. It wasn't selected. So there we go, okay, Tai, Lopez, B, C, D, E, F, G, H, i j, k, l, m, n, o, p, q, r s, t, u, v, w, x bar, some numbers or some other characters.

So You use it. So as an extra task, try and put it into the paint object. So this is what gets thrown to the screen, a little clue, you'll need to use a method called repaint if you want the paint to be run again, you know if you want it to be redrawn again, though, and they will be painful, right, so I'll comment that out. That's how you would invoke that new painting, but only to try and get what you get from the keyboard input, drawing on the screen. That's it for this video. In the next tutorial, we are going to look at Mass events.

If you have any questions or need any help, feel free to reach out and I look forward to seeing you in the next awesome tutorial.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.