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

Author Topic: Write to Console While Out Of Focus/Hidden?  (Read 4995 times)

0 Members and 1 Guest are viewing this topic.

CupertinoEffect

  • Newbie
  • *
  • Posts: 4
    • View Profile
Write to Console While Out Of Focus/Hidden?
« on: December 07, 2015, 12:49:02 am »
I'm trying to figure out if there's a way to write to the console while focused on the window, preferably while the console is hidden.
My goal is to have a line with the function getline(cin, string) that the user can write to without the console being visible.
Is this possible? I'm totally stuck.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Write to Console While Out Of Focus/Hidden?
« Reply #1 on: December 07, 2015, 01:01:27 am »
Why don't you use sf::Event::TextEntered?

CupertinoEffect

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Write to Console While Out Of Focus/Hidden?
« Reply #2 on: December 07, 2015, 01:34:00 am »
Why don't you use sf::Event::TextEntered?
I'm fairly new to SFML so I'm working on what I know from C++.
Is there a way to use sf::Event::TextEntered to input the value of a string?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Write to Console While Out Of Focus/Hidden?
« Reply #3 on: December 07, 2015, 01:43:08 am »
Yes.
If the user types "Hello", the TextEntered event will contain successively H e l l and o. All you have to do is add the typed character to an std::string and you'll have a string containing "Hello".
« Last Edit: December 07, 2015, 01:46:53 am by G. »

CupertinoEffect

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Write to Console While Out Of Focus/Hidden?
« Reply #4 on: December 07, 2015, 01:47:13 am »
Okay, thank you for the quick reply G.

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: Write to Console While Out Of Focus/Hidden?
« Reply #5 on: December 07, 2015, 08:12:56 pm »
It's impossible with console, because it hosted in separate process (out of application). But you can handle input in active window (TextEntered event for sfml) and then you can write your string into redirected CONIN stream. In such way you can handle text input with sfml window and then pass it to the hidden console window, so your function getline will read it from your code instead of console.
« Last Edit: December 07, 2015, 08:18:37 pm by mkalex777 »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Write to Console While Out Of Focus/Hidden?
« Reply #6 on: December 08, 2015, 09:06:21 am »
It's impossible with console, because it hosted in separate process (out of application).
That doesn't make sense -- according to this logic, it wouldn't be possible to work with the console in any C++ application. If you want to explain something, then please don't write a sentence that leaves many more questions open than it answers ;)

and then you can write your string into redirected CONIN stream. In such way you can handle text input with sfml window and then pass it to the hidden console window, so your function getline will read it from your code instead of console.
This is rather confusing. Apparently the only thing CupertinoEffect didn't know was that SFML would also allow text input through the TextEntered event. Hence the attempt to use std::cin -- I doubt he's actually interested in a solution to forcefully use the console just for the sake of it.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

mkalex777

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: Write to Console While Out Of Focus/Hidden?
« Reply #7 on: December 08, 2015, 11:54:51 pm »
That doesn't make sense -- according to this logic, it wouldn't be possible to work with the console in any C++ application.

No, you can use existing console behavior through console api functions, but you will not be able to change something, because console window is hosted in separate process. It means that you will need to inject your code into another process and to implement inter process communication in order to change something ( for example to hook wndproc, add custom menu handler or something like that). It will be much complicated than implementing your own custom console.

« Last Edit: December 08, 2015, 11:57:52 pm by mkalex777 »

CupertinoEffect

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Write to Console While Out Of Focus/Hidden?
« Reply #8 on: December 09, 2015, 05:52:38 am »
Apparently the only thing CupertinoEffect didn't know was that SFML would also allow text input through the TextEntered event. Hence the attempt to use std::cin -- I doubt he's actually interested in a solution to forcefully use the console just for the sake of it.
I can confirm that I was after the TextEntered event and that I was trying to use the console because I'm godawful at SFML.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Write to Console While Out Of Focus/Hidden?
« Reply #9 on: December 09, 2015, 09:56:06 am »
No, you can use existing console behavior through console api functions, but you will not be able to change something, because console window is hosted in separate process.
The very first thing every C++ programmer learns is how to output text on the console (i.e. change its state), and you say it's not possible? I have no idea what you want to tell me, but you should express yourself more precisely ("change something" is a really bad description ;)).

You don't need to do this now, as this is already off-topic (nobody here cares about interprocess communication to consoles), but please keep it in mind when you post in the future :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything