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

  • SEARCH
  • Home
  • 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 8859299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:01:29+00:00 2026-06-14T15:01:29+00:00

Since im a real noob to programming , i was trying to work out

  • 0

Since im a real noob to programming , i was trying to work out this code and i m getting a partial output. The idea was to print the character typed on the applet and if alt or shift key is pressed, it must be displayed in the status bar. The problem is there is no response on the applet but on pressing alt or shift key, i get the appropriate response on the status bar. here is the code:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

/* <applet code="MyKeyApplet.class"
           width = "400"
           height = "400">
   </applet>*/

public class MyKeyApplet extends Applet implements KeyListener{
    char ch;
    String str=" ";

    public void init(){
        this.addKeyListener(this);
        requestFocus();
    }

    public void keyPressed(KeyEvent k){
        int x = k.getKeyCode();
        if(x==KeyEvent.VK_SHIFT)
            showStatus("You pressed Shift Key");
        else if (x==KeyEvent.VK_ALT)
            showStatus("You pressed Alt Key");
    }

    public void paint(Graphics g){
        g.drawString(str,40,40);
    }

    public void keyTyped(KeyEvent k){
        ch=k.getKeyChar();
        str="You Pressed" + ch;
        repaint();
    }

    public void keyReleased(KeyEvent k){
    }

}   
  • 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-14T15:01:30+00:00Added an answer on June 14, 2026 at 3:01 pm

    Haven’t tested myself, but this answer might be helpful: https://stackoverflow.com/a/12483507/877472

    From what I can tell in your code, you’re only ever checking for the modifier key presses (ctrl, alt, shift, etc), but you are not handling any situation for other keys. Again, I haven’t tested this, but you might want to modify your code so that you have two separate handling actions: one for checking if the modifier key is pressed, and then afterwards another for handling ANY key press that is not a modifier. Something like this:

    public void keyPressed(KeyEvent k){
        int x = k.getKeyCode();
        if(x==KeyEvent.VK_SHIFT)
            showStatus("You pressed Shift Key");
        else if (x==KeyEvent.VK_ALT)
            showStatus("You pressed Alt Key");
        else
          str += k.getKeyChar(); // Add the pressed key to the string.
    }
    

    By the by, while I’m not 100% clear on what your program should be doing, however if you plan to continuously build a string, you might find better performance using the StringBuffer class.

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

Sidebar

Related Questions

I apologize if this is not considered real programming since it is in Excel
I'm trying to debug some auto-generated code, but I am a mySQL noob. Everything
Since this doesn't touch a real problem of mine I'm somwhat uncertain, if it
I have a code like this: #!/usr/bin/env bash test_this(){ export ABC=ABC echo some output
I'm an experienced C/C++ Windows developer writing my first real iOS application. Since I
So.. It's already Entity Framework 4.3. I've been waiting for real enum support since
Since I am developing an iOS >= 5.0 application, I am trying to change
Since I have no errors I don't know if this is the right place
I encountered a class during my work that looks like this: public class MyObject
By real pagination i mean something like this when in page 3: <<Previous 1

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.