Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 18
Hi, I've done a little program, it's the snake game. I use an X window to show the snake and all that but the input is taken from the terminal. ...
  1. #1
    Just Joined!
    Join Date
    Apr 2010
    Posts
    7

    Make program get input without the terminal

    Hi, I've done a little program, it's the snake game. I use an X window to show the snake and all that but the input is taken from the terminal.

    That means that what I have to do to play the game is open it with the terminal and then the game opens but then I have to go to the terminal again to move the snake.

    So if I open the game from the GUI I can't do anything because it doesn't take my directions.

    What do I have to do in order to get the program to have a connection with my keyboard?

    I use getchar() to get the input, maybe there is something else?

    If needed I could upload the c files so that you can see for yourselves (=

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    This is done by essentially adding a keylistener onto the window. Then, when a keypress occurs with the window in focus, that keypress is forwarded to your program, and you can take action accordingly.

    The exact manner to do this is dependent on your language and GUI toolkit. What did you use to write this program?
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Apr 2010
    Posts
    7
    I'm using kubuntu. The language is C, gcc compiler.

    Will that work even with my getchar or will I need to change that?

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Yes, but which API library did you use? Kubuntu uses a KDE front-end, so did you use the Qt API's?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Just Joined!
    Join Date
    Apr 2010
    Posts
    7
    To be honest I have no clue. I'm new so I don't know that much =/

    How can I know which I have? ^^

    The following question would then be, if after I've compiled the program and want to test it on a computer running ubuntu, would that mean it wouldn't work?

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    It should work, providing the shared libraries you built it with are compatible with the ones on Ubuntu.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  7. #7
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    In order to create the windows, you have to use a "widget toolkit". In practice, this is usually either Gtk or Qt, but others, such as wxWidgets, or even raw X11, exist.

    Graphics in Linux are handled by something called the X server. X11 libraries talk directly to the X server, whereas Gtk, Qt, and wxWidgets all present a much simpler set of functions to the programmer, and call X11 functions behind the scenes.

    You must be using one of these libraries to make your GUI appear. What function are you calling to create your window? What libraries are you including?

    And to answer the other question, getchar() simply gets a character from the terminal. It does not work for a keylistener.
    DISTRO=Arch
    Registered Linux User #388732

  8. #8
    Just Joined!
    Join Date
    Apr 2010
    Posts
    7
    I think I must be using the X11 actually.
    I use the Xserver to draw in the window and all that.

    I open the window with

    display = XOpenDisplay(display_name);

    These are all the headers I use:

    #include<termios.h>
    #include<unistd.h>
    #include<stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <X11/Xlib.h>

  9. #9
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Ok. You are using the X-libs directly. Talk about doing things the hard way! Ok. Hold on a bit and I'll go get my Xlib reference manual. FWIW, most people use one of the higher level tool kits these days, such as Qt or GTK+. Anyway, I'll be back in a bit to give you some pointers on taking input from your X application.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  10. #10
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    I had to go back to my O'Reilly Xlib Programming Manual to refresh my memory - it's been about 20 years since I last worked at this level of X11...

    Two of the X events you need to process are KeyPress and KeyRelease. You need to first set the keyboard focus when the user has clicked on the appropriate window/sub-window with XSetInputFocus() and XGrabKeyboard(). When you get the KeyPress event, you use the XLookupString() function to get the character that the keypress is mapped to (vs. the keycode, which is not what you want probably, unless you are doing some remapping of the keyboard - sometimes done for "stupid programmer jokes/tricks"). Finally, you use XDrawString() to put the input key in the window. The XLookupString() function will also return the KeySym for the key pressed so that you can do something else, like trap a command key to pull up a diaglog box such as "Save or Exit".

    I know this is very minimal instructions. I have to think that if you have got as far as you have indicated, you have some sort of Xlib documentation available to refer to. If not, here is the Google Books scan of the O'Reilly book I mentioned - see Chapter 9 for keyboard input handling stuff. Note that the Table of Contents seems to be missing the pages for Chapter 9 and 10. I just clicked thru on the last Chapter 8 listing and page forward a couple of pages to get to Chapter 9.

    Xlib programming manual: for version ... - Google Books
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Page 1 of 2 1 2 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
  •  
...