Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7956797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:06:02+00:00 2026-06-04T04:06:02+00:00

Please have a look at the following code import java.awt.event.*; import javax.swing.*; import java.awt.*;

  • 0

Please have a look at the following code

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class KeyCheck extends JFrame
{
    private JButton check;
    private JPanel panel;
    private FlowLayout flow;

    public KeyCheck()
    {
        check = new JButton("Check");
        check.addKeyListener(new KeyWork());

        panel = new JPanel();
        panel.setLayout(new FlowLayout());
        panel.add(check);

        getContentPane().add(panel);

    }

    private class KeyWork extends KeyAdapter
    {
        public void keyPressed(KeyEvent k)
        {
            if(k.getKeyCode()==KeyEvent.VK_CONTROL && KeyEvent.VK_A)
            {
                JOptionPane.showMessageDialog(null, "OK");
            }
        }
    }
    public static void main(String[]args)
    {
        KeyCheck k = new KeyCheck();
        k.setVisible(true);
        k.setSize(200,200);
        k.validate();
        k.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

In this case, I have added an keylister to the button and I need to it to display the Message “OK” when CTRL + A is pressed together (control key and “A” key). But the above code is wrong. So, please help me to get message when both the keys are pressed together.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:06:04+00:00Added an answer on June 4, 2026 at 4:06 am

    You are mixing key code and modifiers:

     if(k.getKeyCode()==KeyEvent.VK_A 
         && (k.getModifiers & KeyEvent.CTRL_MASK==KeyEvent.CTRL_MASK))
    

    But more generally, it is better to use KeyBindings instead of KeyListener. It will make your life a lot easier and avoid you to have to make those kind of tests.

    1.Create an Action like this:

     public class MyAction extends AbstractAction {
    
         public void actionPerformed(ActionEvent e) {
              JOptionPane.showMessageDialog(null, "OK");
         }
     }
    

    2.Bind the action to the key stroke:

    check.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK), "doSomething");
    check.getActionMap().put("doSomething", new MyAction());
    

    Caveats: I haven’t tested this code so may have to fix minor glitches.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please have a look at the following code import java.awt.*; import java.awt.event.*; import javax.swing.*;
Please have a look at the following code. import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.*;
Please have a look at following code : import java.util.ArrayList; import java.util.List; class Main{
Please have a look at the following code import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout;
Please have a look at the following code import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue;
am new here. i have a slight problem; PLease look at the following code
Please have a look at the code below: import string from collections import defaultdict
Please have a look at the following code, which I have run in VB6
Please have a look at the following code: #include <stdio.h> #include <stdlib.h> typedef struct
Please have look on the following code: $_SESSION[process_y] = new Process(); $process_y = $_SESSION[process_y];

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.