1 Replies - 3296 Views - Last Post: 14 June 2011 - 02:47 PM

#1 iSimply  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 29-May 11

user input? My keyPressed() method half works?

Posted 07 June 2011 - 12:37 AM

My code, below, runs but the only keys that are actually added to the vector is 2,4,5,6,8,0, soft1 and 2, send, and clear. I am trying to find 1,3,7,8 what am I doing wrong?

I have throught that maybe because this is extending gamecanvas,and not canvas, but then what should I do?
I would prefer to just use my checkKeys method, and getKeyState to read user input.


    public void checkKeys(int iKey, long currTick){
        long elapsedTick = 0;
        //loop through the keys
        for (int i = 0; i < 12; i++){
            // by default, key not pressed by user
            isDown[i] = false;
            // is user pressing the key
            if ((iKey & wholeKeyValue[i]) != 0){
                elapsedTick = currTick - keyTick[i];
                //is it time to toggle key state?
                if (elapsedTick >= keyDelay){
                    // save the current time
                    keyTick[i] = currTick;
                    // toggle the state to down or pressed
                    isDown[i] = true;
                    
           
                    
                }
            }
        }
    }
    
    public void keyPressed(int keyCode){
      vect.addElement(getKeyName(keyCode));
      //this.repaint();
    }//end keyPressed
    

    
    public void option0(){
        int xFontPos = 50;
        long lCurrTick = 0; // current system time in milliseconds;
        int iKey = 0;
        boolean option0isRunning = true;
        //loadFont();
        //Vector vect = new Vector();
        while(option0isRunning){
            
            lCurrTick = System.currentTimeMillis();
            //iKey = getKeyStates();
            iKey = getKeyStates();                    
            //xFontPos ++;          
            checkKeys(iKey, lCurrTick);
           
                   
            if(isDown[fireKey]){
                 //unLoadFont();
                 vect.removeAllElements();
                option0isRunning = false;
                return;
            }
   
                                     
             //restore the clipping rectangle to full screen
            g.setClip(0, 0, getWidth(), getHeight());
           
            //set drawing color to black
            g.setColor(0x000000);
            //fill the whole screen
            g.fillRect(0, 0, getWidth(), getHeight());
            
            
            
           
            // set drawing color to white 
            g.setColor(0xffffff);
            //display the key code last pressed
            
             String keyData = null;
      int fontHeight = g.getFont().getHeight();
      //Number of lines of text that fit on screen.
      int cntLimit = this.getHeight()/fontHeight;
      int yOffset = 0;
      int xOffset = 0;
            
            
        for(int cnt = 0;cnt < vect.size();cnt++){
        keyData = (String)vect.elementAt(cnt);//note cast

        if(cnt * fontHeight < this.getHeight() - fontHeight){
          //Display in first column.
          yOffset = cnt * fontHeight;
          xOffset = 5;
        }else{
          //Display in second column
          yOffset = (cnt - cntLimit) * fontHeight;
          xOffset = this.getWidth()/2;
        }//end else
        
        //Draw the string on the Canvas.
        g.drawString(keyData,xOffset,yOffset,0);
      }//end for loop

            flushGraphics();
            
            try{
                Thread.sleep(30);
            } catch (Exception ex){
                
                
            }
        }
    }




Is This A Good Question/Topic? 0
  • +

Replies To: user input? My keyPressed() method half works?

#2 iSimply  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 29-May 11

Re: user input? My keyPressed() method half works?

Posted 14 June 2011 - 02:47 PM

For those who struggle with the keypressed/ getKeyState methods, my example above was giving me errors as I had set suppress to true in the constructor of the canvas class.
by changing it to false as seen below

super(false); // true to suppress keys other than game keys. false to allow keypad keys to work


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1