CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 40
  1. #1
    Join Date
    Apr 2007
    Posts
    75

    action on pressing a key

    hi
    i am having an experiment with sensors. I calcualte the current distance top be d0 & print it out. But i need to get a reference point
    So i want to set a reference point everytime i press a key& the distance then has to be caluculated
    with "dref - do" .... & this " new" current distance has to be given out.Does anyone have ideas for the syntax !
    thanks

    Code:
    case WM_PAINT:
    
    ...
    
    ...
    
    d0 = s[1].d + (z_[0] * ( s[2].d - s[1].d )) + (z_[1] * ( s[3].d - s[1].d )) + (z_[2] * ( s[4].d - s[1].d ) )+ (z_[3] * ( s[5].d - s[1].d ));
    
    
    
    
    y += CharHeight*2; /* Print */
    sprintf (OutString,"d current = %lf ", d0);
    TextOut(hDC, x, y, OutString, strlen (OutString));
    
    
    
    
    
    SetTextAlign(hDC, TA_LEFT | TA_TOP);
    EndPaint(hWndMain, &ps);
    break;
    
    
    case WM_KEYDOWN:
    
    if (wParam == 'A') {
    
    
    }

  2. #2
    Join Date
    May 2005
    Posts
    364

    Re: action on pressing a key

    Initially set a reference point to your global static variable before you read the sensor data.
    Then afterwards, use timer or threads to read keyboard input from the user to get a new reference value.

    Always show the (Distance - Ref) on the screen.

    Is this what you are looking for.

    Please explain clearly
    Dan

  3. #3
    Join Date
    Apr 2007
    Posts
    75

    Re: action on pressing a key

    Quote Originally Posted by danandu
    Initially set a reference point to your global static variable before you read the sensor data.
    Then afterwards, use timer or threads to read keyboard input from the user to get a new reference value.

    Always show the (Distance - Ref) on the screen.

    Is this what you are looking for.

    Please explain clearly
    No . Let me try again :-)

    Now i am giving out "d0" continuously on the screen. At a certain point i would like to save a particular value of d0 as my reference value"dref"by pressing a key.
    The value that i would then like to output on the screen is "d0-dref"
    where d0 is the values which i get continuooulsly & was shwoing earlier on teh screen & dref is the value i saved by pressing the key .

  4. #4
    Join Date
    May 2005
    Posts
    364

    Re: action on pressing a key

    Handle the WM_KEYDOWN event and store the immediate value of d0 into a static dref.

    Then from thereon, set a flag that a new ref has come.

    Then always based on the flag's value

    if flag is present
    output d0 - dref
    else
    output d0 // only


    ...Also if you don't want to use the flag set dref initially to 0 and output always d0 - dref

    simple!
    Dan

  5. #5
    Join Date
    Apr 2007
    Posts
    75

    Re: action on pressing a key

    thanks ! i think i will do it the second way ... first dref to 0 & then give dref a value...
    but one question ; how should the key case look :
    Like this ??

    [code]
    case WM_KEYDOWN:

    if (wParam == 'A') {

    d0 = dref ;
    }

    [code]

  6. #6
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: action on pressing a key

    the other way,

    Code:
    dref = d0;
    imho
    Nobody cares how it works as long as it works

  7. #7
    Join Date
    Apr 2007
    Posts
    75

    Re: action on pressing a key

    hm ... this still does not work ... any suggestions ... ??

    Code:
    float dn,dref = 0;
     dn = d0-dref;
    
     d0 = s[1].d + (z_[0] * ( s[2].d - s[1].d )) + (z_[1] * ( s[3].d - s[1].d )) + (z_[2] * ( s[4].d - s[1].d ) )+ (z_[3] * ( s[5].d - s[1].d ));
    
    
    y += CharHeight*2;            
    sprintf (OutString,"d current = %lf    ", dn);           
      TextOut(hDC, x, y, OutString, strlen (OutString)); 
    
    case WM_KEYDOWN:
    
           if (wParam == 'A')  {
    
            dref = d0;
    	 }

  8. #8
    Join Date
    Apr 2007
    Posts
    75

    Re: action on pressing a key

    still not working ... does anyone have an idea ??

    my prob again ::

    Now i am giving out "d0" continuously on the screen. At a certain point i would like to save a particular value of d0 as my reference value"dref"by pressing a key.
    The value that i would then like to output on the screen is "d0-dref"
    where d0 is the values which i get continuooulsly & was shwoing earlier on teh screen & dref is the value i saved by pressing the key .

  9. #9
    Join Date
    May 2005
    Posts
    364

    Re: action on pressing a key

    Which project is this? is it a console project or an Win32 Application project.

    You should handle the WM_KEYDOWN message from windows message handler which are available in the class wizard of MFC.

    If you are using Win32, see MSDN to handle WM_KEYDOWN.
    Dan

  10. #10
    Join Date
    Apr 2007
    Posts
    75

    Re: action on pressing a key

    thanks i will try there. but isnt this the "WM_KEYDOWN" from msdn ?? i was just not sure what has to be written inside the Keydown ... i have called it from my winmessagehandler ...

  11. #11
    Join Date
    May 2005
    Posts
    364

    Re: action on pressing a key

    yes it is!
    Dan

  12. #12
    Join Date
    Apr 2007
    Posts
    75

    Re: action on pressing a key

    thx ! i will try it !
    Last edited by sdherzo; July 3rd, 2007 at 08:58 AM.

  13. #13
    Join Date
    Apr 2007
    Posts
    75

    Re: action on pressing a key

    hm .. i am doing something wrong i teh message handler ... i tried the method suggested by u & set d = d0- dref & set dref initially to 0 & put dref = d0 in the KEYDOWN message habndler ... still doesnt react on pressing the key
    anyone sees the mistake ??

    Code:
    float dref = 0 ;
    
    case WM_PAINT:
    {
     d0 = ...;
    
    
     dn = (d0) - (dref);
    
                y += CharHeight*2;           
    	   sprintf (OutString,"d current = %lf    ", dn);           
                TextOut(hDC, x, y, OutString, strlen (OutString)); 
    
    }
    case WM_KEYDOWN:
    
           if (wParam == 'A')  
    	   
    	  {
    
    
    			
        dref = d0;
    
    	  }

  14. #14
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: action on pressing a key

    You need a break statement after each case in the switch...case block.


    Code:
    case WM_PAINT:
    ...
    ...
       break;
    
    case WM_KEYDOWN:
    ...
    ...
       break;
    Nobody cares how it works as long as it works

  15. #15
    Join Date
    Apr 2007
    Posts
    75

    Re: action on pressing a key

    i have a break ... just didnt post it here as i just wanted to post the important part of the code ;-) The problem lies inside my WM_KEYDOWN i think

Page 1 of 3 123 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured