GK SCIENTIST

General Knowledge One Stop Source
Menu
  • Home
  • Social Science
    • History
    • Political Science
    • Geography
  • Science
    • Physics
    • Chemistry
    • Biology
  • Chemistry Notes
  • Mathematics
  • Computer
  • Tutorial MySQL
  • Tutorial Python
  • Java Tutorial
  • English Grammar
  • English Essay
  • Indian Anthropology
  • Philosophy
  • Solved Paper
  • UPSC
  • Current Content
    • Current Affairs
    • RSTV News
    • Yojana and Kurukshetra Gist
  • Donate
  • Contact Us

If you are interested in advertising to our audience, submit the advertising enquiry form.

Advertising Enquiry
Computer

Java KeyListener

Gk Scientist January 18, 2023 No Comments
Tweet WhatsApp Telegram

Java KeyListener:

We can use the Eclipse IDE for this example. If you do not know about it then follow this link- How to Install Eclipse For Java and create a program in it.

Java KeyListener listens for key events. They are represented by objects implementing the KeyListener interface (located in the java.awt.event package). KeyListener specifies three methods that must be implemented by a KeyListener’s class. These methods have the following signatures.

  • public abstract void keyPressed (KeyEvent e)
  • public abstract void keyReleased (KeyEvent e)
  • public abstract void keyTyped (KeyEvent e)

The keyPressed method is called when a key-pressed event occurs, the keyReleased method is called when a key-released event occurs, and the keyTyped method is called when a key-typed event occurs. To register with a key source, a KeyListener must call the source’s addKeyListener method. (To de-register, a KeyListener would call the source’s removeKeyListener method).

Sample Code for Java KeyListener:

Download the image from here and paste it into the java project folder.

keylistener java code 1 - Java KeyListener
Java KeyListener Code 2 - Java KeyListener
Java KeyListener Code 3 - Java KeyListener

Output: Moveable image. Press keys a, w, s, and d.

Java KeyListener Output - Java KeyListener
import java.awt.event.*;
import javax.swing.*;
import java.awt.Color;

public class Key_listener extends JFrame implements KeyListener{
	
	JLabel label;
	ImageIcon icon;
	
	Key_listener(){
		

		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(400,400);
		this.setLayout(null);
		this.addKeyListener(this);
		
		icon = new ImageIcon("rocket_icon.jpg");
		
		label = new JLabel();
		label.setBounds(0, 0, 100, 100);
		label.setIcon(icon);
		//label.setBackground(Color.GREEN);
		//label.setOpaque(true);
		
		this.getContentPane().setBackground(Color.WHITE);
		this.add(label);
		this.setVisible(true);
	
	}
	
	
    @Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
    	
    	switch(e.getKeyChar()) {
		  case 'a': label.setLocation(label.getX()-10, label.getY());
		    break;
		  case 'w': label.setLocation(label.getX(), label.getY()-10);
		    break;
		  case 's': label.setLocation(label.getX(), label.getY()+10);
		    break;
		  case 'd': label.setLocation(label.getX()+10, label.getY());
		    break;
		    
    	}
		
	}


	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		
		switch(e.getKeyCode()) {
		  case 37: label.setLocation(label.getX()-10, label.getY());
		    break;
		  case 38: label.setLocation(label.getX(), label.getY()-10);
		    break;
		  case 39: label.setLocation(label.getX()+10, label.getY());
		    break;
		  case 40: label.setLocation(label.getX(), label.getY()+10);
		    break;
		}
		
	}


	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
		System.out.println("You released key char: " + e.getKeyChar());
		System.out.println("You released key code: " + e.getKeyCode());
		
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
    new Key_listener();


	}

}

Tutorial Python
Tutorial MySQL
Natural Language Processing (NLP)
Basic Components of Computer System
Characteristics of Computer
Applications of Computer
Java (programming language)– Wikipedia
Prev Article
Next Article

Related Articles

Python Relational Operators or Comparison Operators
Python Relational Operators: We can use the PyCharm code editor …
Gk Scientist November 22, 2022 Computer

Python Relational Operators or Comparison Operators

Logical Operators in Java
Logical Operators in Java: We can use the Eclipse IDE …
Gk Scientist January 1, 2023 Computer

Logical Operators in Java

Create a Clock in Java
Create a Clock in Java: We can use the Eclipse …
Gk Scientist January 19, 2023 Computer

Create a Clock in Java

Python Membership Operators
Python Membership Operators: We can use the PyCharm code editor …
Gk Scientist November 22, 2022 Computer

Python Membership Operators

MySQL Group By and Having Clause
MySQL Group By Clause: The GROUP BY clause groups the …
Gk Scientist November 18, 2022 Computer

MySQL Group By and Having Clause

Python SQLite Update Query
Python SQLite Update Query: We can use the PyCharm code …
Gk Scientist December 9, 2022 Computer

Python SQLite Update Query

Relational Operators in Java
Relational Operators in Java: We can use the Eclipse IDE …
Gk Scientist January 1, 2023 Computer

Relational Operators in Java

MySQL Delete Query
MySQL Delete Query: There are times when some data is …
Gk Scientist November 16, 2022 Computer

MySQL Delete Query

Leave a Reply Cancel Reply

Search




  • Popular
  • Recent




GK SCIENTIST

General Knowledge One Stop Source

Information

  • About Us
  • Terms and Condition, Disclaimer
  • Contact Us

Android Apps

  • IAS App For English Medium Students
  • IAS Basics App For English Medium Students
  • IAS Hindi App For Hindi Medium Students
DMCA.com Protection Status

Popular Tags

Biology (33) Biology Questions (88) Chemistry (57) Computer (213) Current Affairs (4) Current Content (0) Economy (10) English Essay (172) English Grammar (75) English Literature (10) Geography (83) History (259) Indian Anthropology (11) Indian Polity (14) JKAS Mains Question Papers (17) Mathematics (68) Moral Science (7) NCERT & Other Boards Books (25) Philosophy (114) Physics (89) Political Science (132) RS TV News (33) Science (553) Social Anthropology (7) Social Science (17) Solved Paper (47) UPSC (7) UPSC Mains Question Papers (26)

Downloads

  • NCERT Books
  • Old NCERT Books
  • NIOS Books For IAS, SSC, and State PSC Exam
  • Tamil Nadu Board Books: Important For UPSC, SSC, and State PSC Exam
  • Modern Indian and World History Notes For IAS Exam
  • UPSC Topper 2013 Gaurav Agrawal Notes For IAS Preparation
  • UPSC IAS Prelims General Studies – Previous Year Papers
  • UPSC Mains Question Papers

Copyright © 2023 GK SCIENTIST
Theme by MyThemeShop.com& Hosted On Cloudways

Ad Blocker Detected

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Refresh
 

Loading Comments...