[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Detecting joystick activity

Posted on 2000-01-17
7
Medium Priority
?
948 Views
Last Modified: 2010-04-06
I need to know when a PC has been idle for a while. I have a system wide hook for keyboard and mouse activity.

I now have a requirement to do the same thing for the joystick. I need to detect this activity even if my Delphi application does not have the focus.
0
Comment
Question by:moonrise
  • 4
  • 2
7 Comments
 
LVL 6

Expert Comment

by:edey
ID: 2360712
perhaps you could use a Direct Input wrapper? or, if you're not too picky on how long you wait, check for a WM_SYSCOMMAND message with the SC_SCREENSAVE parameter (I believe this gets broadcast to every app when the screen saver starts, though I could be wrong)


GL
Mike
0
 
LVL 17

Accepted Solution

by:
inthe earned 800 total points
ID: 2360859
hi,
this examples that follows is a bit over the top but basically you need add mmsystem to uses then in the message handler you have for keyboard and mouse look also for MM_JOY1BUTTONUP MM_JOY1BUTTONDOWN etc..
Regards Barry


unit JoySetCapture_Form1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, ExtCtrls, mmsystem, ComCtrls, Tabnotbk;

type
  TForm1 = class(TForm)
    TabbedNotebook1: TTabbedNotebook;
    GroupBox1: TGroupBox;
    Panel1: TPanel;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Shape1: TShape;
    Shape2: TShape;
    Image1: TImage;
    BitBtn1: TBitBtn;
    Image2: TImage;
    Image3: TImage;
    Image4: TImage;
    procedure FormActivate(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BitBtn1Click2(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  protected
     procedure joyInit;
     procedure joyToClient(var pptJoyPos: TPoint);
     procedure WndProc(var Msg: TMessage); override;
  end;

var
  Form1: TForm1;
  Threshold: Integer;         // holds the joystick threshold value

implementation

{$R *.DFM}
procedure TForm1.WndProc(var Msg: TMessage);
var
   Cpoint: TPoint;       // holds the joystick position coordinates
begin
  {if the joystick has moved...}
  if Msg.Msg = MM_JOY1MOVE then
  begin
    {retrieve the coordinates relative to the panel}
    Cpoint.X := Msg.LParamLo;
    Cpoint.Y := Msg.LParamHI;
    JoyToClient(Cpoint);

    {modify the Smiley picture based on the position of the joystick}
    if ((Cpoint.x >= 50) and (Cpoint.x <= 55)) and
       ((Cpoint.y >= 40) and (Cpoint.y <= 45)) then
      Image2.Picture.Bitmap.Canvas.CopyRect(Rect(0,0,105,85),
        Image4.Picture.Bitmap.Canvas, Rect(0,0,105,85))
    else
      Image2.Picture.Bitmap.Canvas.CopyRect(Rect(0,0,105,85),
        Image3.Picture.Bitmap.Canvas, Rect(0,0,105,85));

    {draw the crosshair}
    Image2.Picture.Bitmap.Canvas.Pen.Color := clRed;
    Image2.Picture.Bitmap.Canvas.Pen.Width := 2;
    Image2.Picture.Bitmap.Canvas.MoveTo(Cpoint.X - 8 ,Cpoint.Y);
    Image2.Picture.Bitmap.Canvas.LineTo(Cpoint.X + 8 ,Cpoint.Y);
    Image2.Picture.Bitmap.Canvas.MoveTo(Cpoint.X ,Cpoint.Y - 8);
    Image2.Picture.Bitmap.Canvas.LineTo(Cpoint.X ,Cpoint.Y + 8);
    Image2.Picture.Bitmap.Canvas.Ellipse(Cpoint.X - 4 ,Cpoint.Y -4,
      Cpoint.X + 4 ,Cpoint.Y +4);
  end;

  {if a joystick button was pressed...}
  if Msg.Msg = MM_JOY1BUTTONDOWN then
  begin
    {color in a shape depending on which button was pressed}
    if Boolean(Msg.WParam and JOY_BUTTON1) then
      Shape1.Brush.Color := clRed;
    if Boolean(Msg.WParam and JOY_BUTTON2) then
      Shape2.Brush.Color := clRed;
  end;

  {if a joystick button was released...}
  if Msg.Msg = MM_JOY1BUTTONUP then
  begin
    {refill the shape with its original color}
    if not Boolean(Msg.WParam and JOY_BUTTON1) then
      Shape1.Brush.Color := clMaroon;
    if not Boolean(Msg.WParam and JOY_BUTTON2) then
      Shape2.Brush.Color := clMaroon;
  end;

  {send the messages on to the default message handler}
  inherited WndProc(Msg);
end;

procedure TForm1.joyInit;
var
  lpjoyInfoEx: TJOYINFOEX;    // holds extended joystick information
  lpjoyInfo: TJOYINFO;        // holds joystick information
  NumOfDevs: Integer;         // holds the number of joystick devices
  Dev1: Integer;              // holds joystick position return values
begin
  {get joystick threshold}
  JoyGetThreshold(JOYSTICKID1, @Threshold);

  {get number of joystick}
  NumofDevs := joyGetNumDevs;

  {if there are no joystick devices present, indicate an error}
  if  NumOfDevs = 0  then
  begin
    MessageBox(Form1.Handle, 'Joystick driver not present', 'Error',
               MB_OK or MB_ICONWARNING);
    Exit;
  end;

  {determine if there is a joystick present}
  Dev1 := joyGetPosEx(JOYSTICKID1, @lpjoyInfoEx);
  if Dev1 = MMSYSERR_BADDEVICEID then
    MessageBox(Form1.Handle,'Joystick 1 is not present', 'Error ', MB_OK);

  {determine if the joystick is unplugged}
  Dev1 := joyGetPos(JOYSTICKID1, @lpjoyInfo);
  if Dev1 = JOYERR_UNPLUGGED then
    MessageBox(Form1.Handle,'Joystick is unplugged', 'Error ', MB_OK);

  {set the joystick threshold}
  joySetThreshold(JOYSTICKID1, 125);
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
  {capture joystick messages}
  if (joySetCapture(Form1.Handle, JOYSTICKID1, 0, TRUE) <> JOYERR_NOERROR ) then
  begin
    {indicate that there was a problem capturing the joystick}
    MessageBox(Form1.Handle,'Joystick is not captured', 'Error',
               MB_OK or MB_ICONWARNING);
    Close;
  end;
end;

{convert joytick coordinates to client coordinates}
procedure TForm1.joyToClient(var pptJoyPos: TPoint);
var
  JCaps: TJoyCaps;    // holds joystick device capabilities
  CRect: TRect;       // holds window coordinates
begin
  {get joystick capabilities}
  if (JoyGetDevCaps(JOYSTICKID1, @JCaps, SizeOf(TJOYCAPS))<>JOYERR_NOERROR) then
    Exit;

  {set the joystick position relative to the panel}
  Windows.GetClientRect(Panel1.Handle, CRect);
  pptJoyPos.X := TRUNC((Panel1.Width - 1) * (pptJoyPos.X - JCaps.wXmin) /
    (JCaps.wXmax - JCaps.wXmin));
  pptJoyPos.Y := TRUNC((Panel1.Height - 1) * (pptJoyPos.Y - JCaps.wYmin) /
    (JCaps.wYmax - JCaps.wYmin));
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  {initialize joystick}
  Application.ProcessMessages;
  JoyInit;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  {release joystick capture}
  JoyReleaseCapture(JOYSTICKID1);
end;

procedure TForm1.BitBtn1Click2(Sender: TObject);
begin
  Close;
end;

end.
0
 
LVL 17

Expert Comment

by:inthe
ID: 2360863
nboitce these lines in example:

begin
  {if the joystick has moved...}
  if Msg.Msg = MM_JOY1MOVE then ..
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 17

Expert Comment

by:inthe
ID: 2390104
hello ?
0
 

Author Comment

by:moonrise
ID: 2424582
Sorry about the delay, I was away on a trip.  

Question: I wrote WndProc. Are all messages sent to this or only the ones generated by this form/application?

procedure TFormMainMenu.WndProc(var Msg: TMessage);
begin
  if (Msg.Msg = MM_JOY1MOVE) or
     (Msg.Msg = MM_JOY2MOVE) or
     (Msg.Msg = MM_JOY1ZMOVE) or
     (Msg.Msg = MM_JOY2ZMOVE) or
     (Msg.Msg = MM_JOY1BUTTONDOWN) or
     (Msg.Msg = MM_JOY2BUTTONDOWN) or
     (Msg.Msg = MM_JOY1BUTTONUP) or
     (Msg.Msg = MM_JOY2BUTTONUP) then
    ProcessActivityDetected;

  // Send the messages on to the default message handler.
  inherited WndProc(Msg);
end;

0
 
LVL 17

Expert Comment

by:inthe
ID: 2424841
Hi
yep your application will detect all these messages if they are sent globally .. if they are sent specifically to another apps handle only then you may need to write a windows hook to catch them all but i think your winproc will be just fine.
Regards Barry
0
 

Author Comment

by:moonrise
ID: 2427722
Thank you.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question