Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to simulate keyboard/mouse press with SFML?  (Read 6702 times)

0 Members and 1 Guest are viewing this topic.

Ardeshir81

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
How to simulate keyboard/mouse press with SFML?
« on: March 07, 2014, 06:12:33 pm »
HI Everyone !
Is it possible to simulate a key press with SFML ?
I mean can SFML press a key for me ?

The reason I want this is that I'm writing a program which waits for some time (entered by user) and then runs a kernel command . (Linux Ubuntu 13)
But sometimes when the command is "sudo apt-get install ..." it requires a password and a confirmation which is done by pressing 'y' ;
Do you think I'm doing the right thing ?
Is there a better solution to do this or I should keep looking for key press simulation ?


+ :
I tried searching using forum's search BUT I faced this message :

Database Error
Please try again. If you come back to this error screen, report the error to an administrator.

Who is this administrator ?

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: How to simulate keyboard/mouse press with SFML?
« Reply #1 on: March 07, 2014, 06:41:15 pm »
That reads as if you include your root password in plain text inside your program. That is very unsecure and you should never do it. I think there are some other ways that avoid this, maybe try a websearch.

The forumsearch is bugged slightly, you need to choose some subforums to search first as it wont search the whole thing at once.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: How to simulate keyboard/mouse press with SFML?
« Reply #2 on: March 07, 2014, 06:43:57 pm »
Do you think I'm doing the right thing ?
No, you should rather write a script that invokes apt-get install, and start this script using root privileges, i.e. as sudo.

I tried searching using forum's search BUT I faced this message :
Yes, it's annoying. It works if you exclude some subcategories of the forum.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: How to simulate keyboard/mouse press with SFML?
« Reply #3 on: March 07, 2014, 06:52:07 pm »
No. You are not doing it right.
As far as apt-get goes, you don't need to simulate entering "y" via the keyboard; just use the --yes command line argument when you run it (read the man page).
As far as terminology is concerned; that's not a "kernel command" - passing arguments to the kernel is something completely different - see https://www.kernel.org/doc/Documentation/kernel-parameters.txt
If your program/package has dependencies then why don't you simply list them in the .deb package for your app so that the standard package management tools will pull them in, instead of shenanigans like running apt yourself? (I'd advise reading up on proper package building).

Ardeshir81

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: How to simulate keyboard/mouse press with SFML?
« Reply #4 on: March 07, 2014, 07:36:49 pm »
Thanks Everyone!

SO , I'm doing the wrong thing !
I will read on proper package production and hope I get it fixed soon .

BTW can SFML do that ? I mean press a key ?
Or is it impossible and needs hardware (keyboard in this case) to do it ?
Thanks !

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: How to simulate keyboard/mouse press with SFML?
« Reply #5 on: March 07, 2014, 07:39:38 pm »
No, SFML can't do that. You may have to look into libraries specific to your operating system.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: How to simulate keyboard/mouse press with SFML?
« Reply #6 on: March 07, 2014, 07:43:08 pm »
I'm not 100% certain, but I don't think you can inject events (like key press) into the OS's global event queue/event handling from SFML - no.
If you want to simulate key presses etc inside you app, then that's another thing - you can just pretend they happened and do whatever would have happened if a real event had arrived.

Ardeshir81

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: How to simulate keyboard/mouse press with SFML?
« Reply #7 on: March 07, 2014, 07:44:23 pm »
Thanks everyone !

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: How to simulate keyboard/mouse press with SFML?
« Reply #8 on: March 07, 2014, 07:52:27 pm »
You may be interested in xdotool.

There's probably a package for it for Ubuntu. If you want to write a program that does it yourself (good luck), you could check out how it uses the X library and do it yourself.

I'd just write a script that uses xdotool. I did that for the xkcd sandcastle game.
I was bored.