Detecting a keypress 
Author Message
 Detecting a keypress

I am trying to write a simple program in VC++. In order for it to work
at all I need to be able to detect a kepress from within a loop I have
tried the following

void CScribbleView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
//windows message handler
{
 rotate = 1;

 CScrollView::OnKeyUp(nChar, nRepCnt, nFlags);

Quote:
}

void CScribbleView::OnTetrisTetris()
{
     CClientDC pDC( this );

     CScribbleDoc* pDoc = GetDocument();
     CPen* pOldPen = pDC.SelectObject( pDoc->GetCurrentPen( ) );

     CPen blackpen, whitepen;
     blackpen.CreatePen(PS_SOLID, 2, RGB(0,0,0));
     whitepen.CreatePen(PS_SOLID, 2, RGB(255,255,255));

     int y1,y2;

     for(int vpos=1; vpos < 10; vpos++)
     {
        pDC.SelectObject( blackpen );
        pDC.MoveTo( x1,10 );

        y1 = 10 * vpos;
        y2 = 10 + 10 * vpos;

        pDC.Rectangle(x1,y1,x2,y2 );

        for(int a=1; a<10000000; a++)
        {

            if(rotate==1)
           {
                x1 += 10;
                x2 += 10;
           }
      }

        pDC.SelectObject( whitepen );

        pDC.Rectangle(x1,y1,x2,y2 );
        }

    pDC.SelectObject( pOldPen );

Quote:
}

What the above is supposed to do is draw a rectangle at progressively
lower postions on the screen and then delete them (this bit works fine)
If a key is pressed then the next rectangle should be drawn to the right

of the last and below. It doesn't do that however, key presses are
ignored until after the call to OnTetrisTetris.

Could anyone help with this, any advice would be much apprieciated.



Sun, 03 Sep 2000 03:00:00 GMT  
 Detecting a keypress

Quote:
>I am trying to write a simple program in VC++. In order for it to work
>at all I need to be able to detect a kepress from within a loop I have
>tried the following

You can detect a key press by using GetAsyncKeyState. However in your
situation it might be better to restructure your program so that it
does successive actions on a timer event rather than in a tight loop.
In that case normal Windows message handling will be effective.

Dave
----
Address is altered to discourage junk mail.
Remove ".---" for the real address.



Sun, 03 Sep 2000 03:00:00 GMT  
 Detecting a keypress

Quote:

> You can detect a key press by using GetAsyncKeyState. However in your
> situation it might be better to restructure your program so that it
> does successive actions on a timer event rather than in a tight loop.
> In that case normal Windows message handling will be effective.

Thankyou Dave. That worked well . I'll try the timer event driven
version later.


Mon, 04 Sep 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Detecting A Keypress

2. HOW TO: detect a keypress

3. Detecting delete keypress in OnChar

4. Detecting keypress

5. HELP! Keypress detect

6. How To Detect KeyPress.

7. Detecting keypress when the focus is on CButton

8. Detect keypress in dialog-based

9. combobox and keyPress Event

10. Convert keypress to upper case

11. object sender keypress (c# newbie)

12. Tab KeyDown-KeyPress trapping

 

 
Powered by phpBB® Forum Software