Results 1 to 3 of 3
  1. #1
    linkinlog0123 is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default Help with on screen keyboard Capslock

    i have been making an on screen keyboard for my intro to JAVA class. the keyboard is very basic but, no matter what method i try i cannot get the toggle caps-lock button to work.
    in the l.getSource() area for the bottom row(third loop) the 10th button in the array(9th by index) is the caps lock button, but no matter where i try to re-initialize the strings, they either have an error, or don't work as lower case letters
    Java Code:
    import javax.swing.JOptionPane;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JButton;
    import javax.swing.*;
    
    public class Keyboard extends Frame implements ActionListener
    {
    	int place,length = 0;
    	int tries=0;
    	boolean capslock = true;
    	String topRow[] = {"Q","W","E","R","T","Y","U","I","O","P"};
    	String midRow[] = {"A","S","D","F","G","H","J","K","L","Enter"};
    	String botRow[] = {"Space","Z","X","C","V","B","N","M","Back","Lower"};
    	Button top[] = new Button[10];
    	Button mid[] = new Button[10];
    	Button bot[] = new Button[10];
    	Panel keys = new Panel();
    	Panel text = new Panel();
    	TextArea tArea = new TextArea();
    	String strText = "";
    
    	public Keyboard()
    	{
    		this.setLayout(new BorderLayout(1,1));
    		keys.setLayout(new GridLayout(3,10));
    		text.setLayout(new BorderLayout(1,1));
    		text.add(tArea);
    
    		for(int i=0; i<10; i++)
    		{
    			top[i] = new Button(topRow[i]);
    			top[i].setBackground(Color.white);
    			keys.add(top[i]);
    			top[i].addActionListener(this);
    		}
    		for(int i=0; i<10; i++)
    		{
    			mid[i] = new Button(midRow[i]);
    			mid[i].setBackground(Color.white);
    			keys.add(mid[i]);
    			mid[i].addActionListener(this);
    		}
    		for(int i=0; i<10; i++)
    		{
    			bot[i] = new Button(botRow[i]);
    			bot[i].setBackground(Color.white);
    			keys.add(bot[i]);
    			bot[i].addActionListener(this);
    		}
    		add(text, BorderLayout.NORTH);
    		add(keys,BorderLayout.CENTER);
    		addWindowListener(
    				new WindowAdapter()
    				{
    					public void windowClosing(WindowEvent e)
    					{
    						System.exit(0);
    					}
    				}
    				);
    	}
    	public static void main(String[] args)
    	{
    		Keyboard f = new Keyboard();
    		f.setBounds(100,100,500,400);
    		f.setTitle("Keyboard");
    		f.setVisible(true);
    	}
    
    	public void actionPerformed(ActionEvent l)
    	{
    	
    		for(int i=0;i<10;i++)
    		{
    			if(l.getSource() == top[i])strText = strText+topRow[i];
    		}
    		for(int i=0;i<10;i++)
    		{
    			if(l.getSource()== mid[i])
    			{
    				if(i == 9) strText = strText+"\n";
    				else strText = strText + midRow[i];
    			}
    		}
    		for(int i=0;i<10;i++)
    		{
    			if(l.getSource () == bot[i])
    			{
    				if(i == 0) strText = strText+" ";
    				if(i == 8)
    				{
    					length = strText.length();
    					strText = strText.substring(0,(length-1));
    				}
    
    					else
    					{
    						if(i == 9)
    						{
    							if(capslock == true) capslock = false;
    							if(capslock == false) capslock = true;
    						}else strText = strText +botRow[i];
    					}
    				}
    
    			}tArea.setText(strText);
    		}
    	}
    Last edited by linkinlog0123; 11-17-2010 at 04:36 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default

    I'll take a look at the rest when I get some time, but this code is horribly convoluted
    Java Code:
    if(capslock == true) capslock = false;
    if(capslock == false) capslock = true;
    What you want to do is
    Java Code:
    capslock = !capslock;
    I would use a JToggleButton for caps lock and any other keyboard buttons that toggle a value between true and false.

    Also, consider using key bindings for the majority of your onscreen keys.
    How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
    Key Bindings Java Tips Weblog

    db

    edit Scratch the last two, I hadn't noticed you're using AWT, not Swing. May I ask why you are using such an outdated technology when a newer toolkit is available?

  3. #3
    linkinlog0123 is offline Member
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default

    I'm using AWT because for this project we have to use it, and i have more knowledge using awt rather than swing.

    i also see why you think the code is convaluted, that was the 10th attempt at changing the toogle caps lock delima, and there was a link to a method that i deleted, I just forgot to delete those 2 lines

Similar Threads

  1. getting each character from keyboard
    By Sreejesh25 in forum New To Java
    Replies: 11
    Last Post: 01-25-2011, 02:08 PM
  2. Blank Screen while navigating from one screen to another
    By mohana.krishna in forum Java Servlet
    Replies: 0
    Last Post: 03-03-2009, 06:03 PM
  3. Reading Keyboard
    By ushigley in forum New To Java
    Replies: 4
    Last Post: 08-20-2008, 02:50 PM
  4. Need help with on-screen keyboard (first post)
    By tom_k in forum New To Java
    Replies: 7
    Last Post: 07-31-2008, 03:07 AM
  5. Help with keyboard events?
    By Bibendum in forum New To Java
    Replies: 2
    Last Post: 11-02-2007, 03:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •