Thread: Request for Code for Joystick

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    3

    Question Request for Code for Joystick

    hi all, I am going to write a program which need a joystick as my input device. This is my first time doing such task. I'm not experienced in this kind of input method.

    Any kind senior can tell me what is the code for a joystick? According to my knowledge in C++, every key on keyboard also has a specific code, and what will it be for a joystick? (or maybe my concept is wrong, if it is, please tell me so that i can learn something)

    Waiting for your reply.

    Thank you.. ^.^

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Nothing to do with networking. Moved to game programming.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Your understanding of how joysticks work is wrong. It's not like the keyboard where standard library functions just read numbers from some file like stdin through std::cin. The reason being is that all I/O is wrapped by the O/S since these days you almost never directly read or write to hardware (exception for people that write drivers and such). Even regular I/O through stdout and stdin are done via the O/S. It's the C and C++ libraries that wrap the O/S specific functions, so simple console-based I/O can be done the "same way" on multiple platforms. Since data processing back in the day for terminals didn't require joystick input, they left that out of the standard libraries.

    Low level, reading from a joystick is the same idea as reading from the keyboard in the sense that it's just another device to read from, but not from your perspective as a C++ programmer due to there being no support in the C++ standard libraries. This means you need to figure out which O/S you're working on, and learn how to read from devices such as joysticks in the specific O/S manner. If you're on Windows, for example, DirectX is what you would need to learn, or at least the DirectInput portion of it I believe.

    Another alternative is to get a library like Allegro that wraps a lot of the O/S specific items of game development and lets you do things through their library. I believe Allegro has specific support for joysticks, although I'm not sure how detailed.
    Last edited by MacGyver; 05-02-2007 at 09:00 AM. Reason: Clarification.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Isn't Allegro just a graphics library?

    I'd take a look at SDL, it has joystick support.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The question is moot, since the answer depends on the compiler / OS / graphics library that the OP is actually using.

    So dreamer77, what are the relevant system details?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    You migth try looking into DirectInput as well, I seem to recall quite a bit of support there for joysticks, including force feedback stuff. Its part of the DirectX SDK which is availabel for MS VC++ 6.0 or Bloodshed, probably others as well.

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    3
    Quote Originally Posted by Salem View Post
    The question is moot, since the answer depends on the compiler / OS / graphics library that the OP is actually using.

    So dreamer77, what are the relevant system details?
    I'm planning to write a program, which will read the movement of joystick and send a signal out to move the control panel of my aircraft. This is Final Year Project of my school. My idea is:

    Joystick>>>PC>>>Digital to Analog Converter>>>Aircraft Control Panel

    Therefore, the program is mainly to read the input of joystick, and take the coresponding respond. I'm planning to write this program using C++. However, I think later some other sofeware mayb needed in the Digital to Analog Converter's circuit.

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    3
    Mac, thx for ur help. I need sometime to digest your reply, cause I'm noob in C++. LOL.

    Again, thx for ur help.

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    If you do not understand how the old joysticks worked you will have some trouble understanding how DirectInput gives you access to them. There are several types of gamepads/sticks and some are digital, some analog, and some both.

    DirectInput is quite abstract when it comes to dealing with joystick/gamepad input and as well it should be since there are about a billion different types out there. And the post about looking at joystick input like keyboard input is not completely accurate as you can assign certain buttons and axes to 'events' which act just like the keyboard. I would recommend reading the DirectInput section of your SDK and read it entirely. There is a lot of information crammed into it but it is going to probably be the only way to explain it to you. I could not and will not attempt to explain the complexities of DirectInput and devices here in this post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with client GET request path
    By NuNn in forum C Programming
    Replies: 1
    Last Post: 02-25-2009, 03:34 PM
  2. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  3. my HTTP request handler code correct?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-25-2008, 04:01 AM
  4. denied request
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 09-20-2001, 11:35 PM