An advanced KeyListener for Java Swing UI.

Overview

keystrokelistener

An advanced KeyListener for Java Swing UI.

In Swing, We don't have any default way of mapping a set of KeyStrokes to a specific task.

Or I should say We didn't had one earlier but now, it is possible with KeyStrokeListener.

This implementation is dynamic in nature, this means

  • The KeyStrokeListener only performs checks for the specified keys.
  • Thus, it is faster than any other custom implementation of KeyListener.

Mapping Set of KeyStrokes with a runnable is now possible without creating a custom implementation.

Either use the source code or download the tiny precompiled-jar.

Required Java Version

If you want to use this library through the jar then you need to have at least 17 as the compilation level.

Simply, you need java 17 if you want to use this library through the jar.

Usage

This API is super simple to understand.

  • Just create a KeyStrokeListener object by passing the component object in the constructor
  • Use putKeyStroke method to map a task with a keystroke

Like below

{ //This block executes only if CTRL, SHIFT & S Keys are pressed //But this block will also execute even if any other key was //also pressed in combination with specified keys like CTRL + ALT + SHIFT + S System.out.println("You pressed CTRL+SHIFT+S"); }, VK_CONTROL, VK_SHIFT, VK_S); listener.putKeyStroke((e)->{ //This block executes only if CTRL & S Keys are pressed //But this block will not execute if the specified stop key //is pressed in combination with the specified keys System.out.println("You only pressed CTRL+S and not SHIFT key"); }, VK_CONTROL, VK_S).setStopKeys(VK_SHIFT); listener.putKeyStroke((e)->{ //This block executes if CTRL, SHIFT & V Keys are pressed and uses autoReset() //Use the useAutoReset() method only if this block does something which //Makes the textArea component to lose focus //Like in case of showing a dialog System.out.println("You only pressed CTRL+SHIFT+V and I cleared the specified key's cache"); }, VK_CONTROL, VK_SHIFT, VK_V).useAutoReset(); f.setVisible(true); } }">
import javax.swing.JFrame;
import javax.swing.JTextArea;

import omegaui.listener.KeyStrokeListener;

import java.awt.*;

import static java.awt.event.KeyEvent.*;

class TestWithTextArea {
    public static void main(String[] args) {
        JFrame f = new JFrame("KeyStrokeTest");
        f.setSize(500, 400);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextArea textArea = new JTextArea("Just a text area!");
        f.add(textArea, BorderLayout.CENTER);

        KeyStrokeListener listener = new KeyStrokeListener(textArea);
        listener.putKeyStroke((e)->{
            //This block executes only if CTRL, SHIFT & S Keys are pressed
            //But this block will also execute even if any other key was
            //also pressed in combination with specified keys like CTRL + ALT + SHIFT + S 
            System.out.println("You pressed CTRL+SHIFT+S");
        }, VK_CONTROL, VK_SHIFT, VK_S);
        listener.putKeyStroke((e)->{
            //This block executes only if CTRL & S Keys are pressed
            //But this block will not execute if the specified stop key
            //is pressed in combination with the specified keys
            System.out.println("You only pressed CTRL+S and not SHIFT key");
        }, VK_CONTROL, VK_S).setStopKeys(VK_SHIFT);
        listener.putKeyStroke((e)->{
            //This block executes if CTRL, SHIFT & V Keys are pressed and uses autoReset()
            //Use the useAutoReset() method only if this block does something which
            //Makes the textArea component to lose focus
            //Like in case of showing a dialog
            System.out.println("You only pressed CTRL+SHIFT+V and I cleared the specified key's cache");
        }, VK_CONTROL, VK_SHIFT, VK_V).useAutoReset();

        f.setVisible(true);
    }
}

Execution of above example

Running the above code will create a window containing text area

In the above example, the e in the lambda expressions is the object of KeyEvent class

If you press

  • Ctrl + SHIFT + S you will see You pressed CTRL+SHIFT+S as output
  • Ctrl + S you will see You only pressed CTRL+S and not SHIFT key as output
  • Ctrl + SHIFT + V you will see You only pressed CTRL+SHIFT+V and I cleared the specified key's cache as output
You might also like...

Java Desktop (JavaFX and Swing) Component Inspector

Java Desktop (JavaFX and Swing) Component Inspector

Java Desktop (JavaFX and Swing) Component Inspector A Tool for help you to inspect the location and properties of certain components in a window hiera

Oct 28, 2022

Yet another very simple java 3D software renderer using only standard 2D libraries (Java2D, AWT & Swing). Video: https://youtu.be/hWUX1t9f6zE

Yet another very simple java 3D software renderer using only standard 2D libraries (Java2D, AWT & Swing). Video: https://youtu.be/hWUX1t9f6zE

Another very simple 3D software renderer Another very simple 3D software renderer implemented in java using only standard libraries (java 2D, AWT & Sw

Oct 17, 2022

PostgreSQL is the world's most advanced open source database. Also, PostgreSQL is suitable for Event Sourcing. This repository provides a sample of event sourced system that uses PostgreSQL as event store.

PostgreSQL is the world's most advanced open source database. Also, PostgreSQL is suitable for Event Sourcing. This repository provides a sample of event sourced system that uses PostgreSQL as event store.

Event Sourcing with PostgreSQL Introduction Example Domain Event Sourcing and CQRS 101 State-Oriented Persistence Event Sourcing CQRS Advantages of CQ

Dec 20, 2022

Aether - An advanced sync plugin for Minecraft.

Aether - An advanced sync plugin for Minecraft.

Aether Aether is an advanced sync plugin for Minecraft. Aether uses MongoDB for storing player information. Found a issue(s)? Report them in our issue

Sep 6, 2022

XR3Player - The MOST ADVANCED JavaFX Media Player

 XR3Player  - The MOST ADVANCED JavaFX Media Player

Support me joining PI Network app with invitation code AlexKent I am in search for developers to keep on where i left XR3Player :) XR3Player ( Downloa

Dec 17, 2022

Everything I code in java / Learn in Java I will post here to show my Progress :)

Everything I code in java / Learn in Java I will post here to show my Progress :)

This repository contains all the codee i have written or used to help me learn This is going to be a repository that holds the source files for codene

Jan 10, 2022

A Java framework for creating sophisticated calendar views (JavaFX 8, 9, 10, and 11)

A Java framework for creating sophisticated calendar views (JavaFX 8, 9, 10, and 11)

CalendarFX A Java framework for creating sophisticated calendar views based on JavaFX. A detailed developer manual can be found online: CalendarFX 8 D

Jan 6, 2023

Controls for adding Parallax effects for Java (JavaFX)

Controls for adding Parallax effects for Java (JavaFX)

FXParallax Parallax framework for Java (JavaFX). This framework adds controls to add Parallax effects to JavaFX application, this effect can add a sen

Sep 30, 2022

Ribbon control for Java, created in JavaFX

Ribbon control for Java, created in JavaFX

FXRibbon Ribbon control for Java, using JavaFX framework, based on Microsoft Ribbon. If you want to support the development of this library consider a

Dec 27, 2022
Releases(v1.1)
Owner
omega ui
Adding Feathers to Development
omega ui
An advanced book explorer/catalog application written in Java and Kotlin.

Boomega An advanced book explorer/catalog application written in Java and Kotlin. ✨ Features Cross-platform Dark/Light theme, modern UI Multiple UI la

Daniel Gyoerffy 54 Nov 10, 2022
A 3D chart library for Java applications (JavaFX, Swing or server-side).

Orson Charts (C)opyright 2013-2020, by Object Refinery Limited. All rights reserved. Version 2.0, 15 March 2020. Overview Orson Charts is a 3D chart l

David Gilbert 96 Sep 27, 2022
A cross-platform interface for FutureRestore, written in Java with Swing

Futurerestore GUI A GUI implementation for FutureRestore written in Java. Installation Download from releases. No Java download required (it's bundled

null 428 Dec 30, 2022
Tictactoe in java swing

TicTacToe A Tictactoe game in java swing Playing You can play with your friend or computer but currently compuer is implemented as Random, In future m

Sri lakshmi kanthan P 3 May 25, 2021
Jogo da cobrinha feito em java swing.

Jogo da Cobrinha Introdução Esse é o classico jogo da cobrinha. v1.0.0 Como configurar Pre-requisitos Java 11, Maven; Procedimentos para rodar projeto

Rodrigo Guedelho 4 Dec 2, 2022
A 2D chart library for Java applications (JavaFX, Swing or server-side).

JFreeChart Version 2.0.0, not yet released. Overview JFreeChart is a comprehensive free chart library for the Java(tm) platform that can be used on th

David Gilbert 946 Jan 5, 2023
A hotel management system desktop application, built with java SWT/Swing

A hotel management system desktop application, built with java SWT/Swing, with tabs to manage bookings, rooms, customers, payments, cancellations, hotel inventory orders, catering and a plan to visualize all the reservations for the month.

null 1 Jan 12, 2022
Swing (Java) Practicing.

Getting Started Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code. Folder Struct

Poom Yimyuean 2 Nov 3, 2022
Java Swing application to upload files to FTP server with progress bar

Java Swing application to upload files to FTP server with progress bar Swing-based application that uploads files from local computer to a remote FTP

Aditya Deshmukh 1 Feb 11, 2022
This is a clone of Mircosoft Paint that uses Java and its javax.swing library

PaintClone This is a clone of Mircosoft Paint that uses Java and its javax.swing library You are able to select a RBG colors and creates a pallet of t

Nick Quinones 2 Feb 17, 2022