Thread: there is a function to auto press a key?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    18

    there is a function to auto press a key?

    I want something like this in a program running on linux:

    Code:
    if ( ok() )
         {
              <code>
         }
    where <code> is a function to automatically press the key "C" in the keyboard, and the program "think" its me pressing the key.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You mean so the key goes down like a ghost was there?

    What you are asking for could mean different things depending on the context. What/where/why do you want this?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You'd need to use a Linux API call similar to the SendInput() function on Windows. I'm not a Linux expert, so I'd have to search for it just like you.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    18
    You mean so the key goes down like a ghost was there?

    What you are asking for could mean different things depending on the context. What/where/why do you want this?
    well, im trying to edit the source code of an emulator, to accept a key only if ok() allow, but the emulator accept the keys and buttons everywhere, so dont matter the content of ok(), if i press the "c" key (or the button 7 in joystick for example) the emulator accept the button every time, so i was thinking the only way is the own program press the key when the ok() function tells to


    You'd need to use a Linux API call similar to the SendInput() function on Windows. I'm not a Linux expert, so I'd have to search for it just like you.
    i found this page:

    http://linux.die.net/man/

    but every section have A LOT of commands...

    edit:

    i think i found something:

    http://help.lockergnome.com/linux/si...ict487117.html
    Last edited by fbs777; 03-23-2009 at 11:04 AM.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    18
    well, using the comand on terminal:

    xvkbd -text c

    he returns the letter "c" on terminal, but if i try this:

    Code:
    if ( ok() )
         {
      	        if ( event.jbutton.button == 7 )
      	        {
    			system("xvkbd -text c");
       	        }
         }
    when i press the button 7 the results of system() (the 'C" keypressed) go to terminal, not the program itself...

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I hate to deal a crushing blow, but it sounds kind of like you are in over your head; if you are trying to alter some source, I really doubt this is the appropriate method to accomplish your ends (but I haven't seen that source either). This is a bit of a guess(es): is this an SDL program (I thought I saw that in an earlier post but you have edited it out) -- I've never used SDL but I would assume you are trying to short circuit (ie, trip) some kind of signal handler function, because SDL must use them.

    If you don't know what I mean by signal handler function, you simply do not have enough C prowess to satisfy your programming desires today, but you might get there by the end of the week.

    If you do know what I mean and think I'm an upstart fool, ignore this post
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    18
    sorry, on the last edit to include the links i forgot to put all again... yes, is the sdlmame and use sdl.

    i will see the source code of the xvkbd to see how he send the signal, and try to put in the emulator the same code instead using the system()

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by fbs777 View Post
    i will see the source code of the xvkbd to see how he send the signal, and try to put in the emulator the same code instead using the system()
    Again, I am sure you are going the wrong way! How familiar are you with the SDL API (I am guessing not very)? "how he sent the signal" vis, system calls or whatever is going to be by intention OPAQUE; the point of the API is to give you functions for this, and I would bet money that is how the code you are looking at works. Otherwise you might as well write your own SDL from scratch (that is the way you will be going by resorting to the xvkdb source) which will make modifying some piece of code WRITTEN FOR USE WITH SDL irrelevent.

    It will be IMPOSSIBLE to modify if you do not have enough experience with SDL (as I said, I do not, but I at least understand this much about how it must operate).

    You do not need to look at or understand xvkbd.
    You do not need to simulate a keyboard event at all.
    You DO need to understand C and SDL.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Registery with api?
    By ismael86 in forum Windows Programming
    Replies: 1
    Last Post: 05-14-2002, 10:28 AM