Prompt user to fill in a template? - c++

I've never been able to find anything about this in any language, but what I want to do seems rather simple to me.
I want to prompt the user for input, but have them fill in a sort of template. Let's use a simple DD-MM-YYYY date as an example.
[█ - - ]
█ is where the user writes.
As they write, the [ and - stay where they are, with the cursor jumping past them, and hitting enter submits.
I'm not expecting anyone to write all of this for me, but cin and cout seem to be too crude a tools to do this, and so I'm wondering where I should even start, if not there.
Is there something that already does this, or at least something that gives me more control of the cursor to allow me to write it myself?
I do not want to be drawing a full-on TUI with windows and boarders and colors, so ncurses would just get in the way. I just want a single-line text field that returns the input.
EDIT: The most promising non-ncruses alternative for a helpful library seems to be Terminal.

Generally what you want is a text-based GUI library, such as ncurses. Doing console work is platform-specific, and every system has its own console API to do this. If you want to implement this yourself, you would have to examine what options does your target operating system give you in terms of console API, and build a custom solution based on that.

Related

Moving cursor using code and opening folder

This is a very random and maybe a bit strange question that i thought of at 3AM. I was thinking about how code could make my day to day life easier. Every morning I wake up, open chrome to the facebook conversations with my boyfriend, and write "good morning". And thats when i thought about this hypothetical project(just out of curiosity, I wouldn't use it haha): making a code that i can just run that does all of this for me.
I could have a html file that could redirect to the facebook link(https://www.facebook.com/messages/t/boyfriend_name). But how would I go on to make the code open this file, then move the mouse to where its supposed to go (the white area where the user inputs the text) then insert the text then press send?
I'm not asking for any code help as I can imagine that is too much, but my question is: could this be achievable in C++?(This is what we've been studying at school so far). If not, what coding language should I use? Is the idea achievable without a vast knowledge in computer science? If yes, have you got any sources about opening files using C++, moving cursor etc.
Note:The OS this would happen on is Windows 10
To do what you want is possible by using AutoIT and to use it from C++ you can try AutoITX for C++. With AutoIT it's possible to detect windows, move the mouse and insert text, although a web page is like a blackbox to it, so you'll have to rely on relative pixel coordinates (it might not be very robust).

How can I get direct keyboard input in cpp? (cross platform)

I want to be able to control the program with direct input without pauseing the program and without waiting for the user to press enter.
I can currently achieve this with kbhit, but it appears that is a windows only solution.
Ncurses also looks promising, but it appears to only work for linux (and maybe mac? Is unix the structure mac is based on?).
I especially like the kbhit for it's simplicity, and I like the tool set that ncurses, but I was wondering if there was a simple, easy way to get something running cross-platform without having to maintain essentially 2-3 versions of the code using these different tools?
I figured out my problem. I'm trying to do many things that cpp was never intended to do by default, so the options are to use libraries like allegro, or, in my case it turns out what I needed was a game engine. I found that Unity with text mesh pro can do everything I need it to. It comes with an entire input management system.

How to draw to a certain part of the console/terminal?

I'm curious to know how you can draw/update a certain region of the terminal/console. Is there any cross-platform libraries to do so?
The reason I want to know is because I am developing an instant message command line application, and I was curious to know how I can update the message viewer (where all the messages go) separately to where you write commands/text for other people in the chat. Obviously if I just tried to get input and write to cout then the input the user is entering and the messages would be "interfered" (by interfered I mean split in multiple lines).
I was thinking of using two stream objects: one to store the view (messages/output from the server) and one to store the input from the user, and just redraw whenever required. However, this seems inefficient and it requires me to clear the screen (in which case I don't know how to clear the screen efficiently and in a cross-platform manner).
I was also thinking of just switching to Qt/wxWidgets as it might simpler to make a GUI.
Use ncurses library to write text-based user interfaces in a terminal-independent manner.
As suggested by #Naruto, ncurses is a good way to go. At a much more basic level, you can also just use ANSI escape codes to move the cursor around the screen too:
For example, to position the cursor at line 5, column 23, you can enter this
echo -n "\033[5;23H"
There are more examples here.

Keyboard input in games (for GLUT)

Almost every game use keyboard as input. I have been searching for 2 days on this topic and found quite much about it. Keyboards have many disadvantages, but main problems I found are different layouts and second that if you are pressing 3 keys at time, it can lead to corruption (row-column error). If you don't know what I'm talking about, keyboard is made as grid and it checks which row and column has connected. But if you press E,D (row 1,2 column 3)
and R (row 1, column 4), keyboard can show even F because it find it pressed(row 2, column 4 both pressed).
So I think we can't do anything about that second, but if anyone got idea how to solve it better then use keys that don't form L, I'd be glad :)
But my main problem are different keyboard layouts which is real pain. I'm slovak so Slovak layout of numbers look like this:
+ľščťžýáíé and with shift 1234567890, we also got QWERTZ but you can use QWERTY.
You all know how the English look like but just for sure:
1234567890 and shift !##$%^&*()
Most of time I'm using english one because I got used to it when programming. Anyway there are different people using different layouts. When you are making game that depend on which key is pressed, for example good old WASD pattern, you can't use that on french one which is AZERTY layout. It would be strange. Same as using numbers for choosing gun in action game. As you can see slovak would have to press shift to get it work.
I'm also using OpeGL. There is problem when you are mapping which keys are pressed. For example widely used solution to make map of 256 bools for each charakter, is suffering from SHIFT. You press a, SHIFT and release a you got: a down, A up. So I thought about binding some keys together, as A and a, 1 and !, but then I realized I'll just change layout and everything is wrong.
So what is solution for that? I think there is someone out there that is in game industry or made some game and had to solve this. Only solution that comes on my mind is to force english layout for UI (and choosen layout for chat).
After next searching I found what I need but I need cross-platform one:
virtual key codes
And next search revealed SDL key
Result: Don't ever start with GLUT if you go making games, use SFML or SDL
Thanks to everyone for helping me, there were more problems in this so idea of key binding/mapping, SDL and so on, each helped me alot.
If you're getting a "character" every time user presses something, then your keyboard routines aren't suitable for game input - only for text entry.
You need to use input routines that completely ignore keyboard layout switching and operate on some kind of raw keycodes (so when user presses shift+a, you'll know that shift is pressed and that "a" key is pressed, but you won't get "A" character). Also, routines should allow you to query if a key is pressed or not.
For windows that is DirectInput or XInput. For cross-platform this is libsdl and SDL_GetKeyState. And you'll need to provide keymapping options for the user. Glut probably isn't suitable for your task.
The common approach seems to be to ignore the problem. Worse-is-better in its early stage.
Unfortunately I'm using svorak keyboard layout so it really doesn't just work for me.
I've been approaching this same problem by binding into multiple keys on the keyboard. So that player jumps from both x and j -keys. It doesn't do so well in something that isn't shoot-jump -kind of game.
Nice stuff would be if you could just find row/col or some driver-near interface to your keyboard.
Some auto-keyboard configuration software would be neat though I've not yet seen anything like that. Maybe we should write one?
First up, separate keypresses from text entry. You shouldn't care what letter or number comes up when you press a key with shift as well - the operating system should handle that and generate an event you can use in the rare times you need the text. Usually, you should just look for the key press and any shift presses and act on those.
Second, load the bindings from keys to commands from a data file, rather than hardcoding it. Distribute default bindings for QWERTY and whatever default layout you have. If the data format is quite straightforward then people won't mind customising it to fit their keyboard and preferences. You can also add an in-game keybinding editor later.
This isn't really about OpenGL since by default that doesn't care about keypresses. Perhaps you are using an addon library or extension that handles keys for you - ensure that whatever you're using can give you individual key values and the state of shift/alt/ctrl independently, and that it also provides text input via an independent system.
Allow your users to define the keys to use for each action ...
or use the arrow keys .. that should be pretty universal :)
Turn the keyboard input into metadata, so you could allow users to configure at their will but also provide different keyboard shortcuts depending on the keyboard layout used in a config file .

(C++) Can you add a direct link into a C++ program without a GUI?

I was wondering if you can add a link to a website in a C++ program running in the CMD Prompt type window (No GUI)
If it's possible can some one please give me a few examples?
You mean output text in the command prompt that the user can then click on? No, not unless the terminal supported it. Linux terminals usually autolink text that matches a URL pattern, so you could just printf("http://stackoverflow.com/\n"); and it would be clickable, but that's up to the terminal, not your program
When you write 'direct link' it is not clear if you mean clickable text or a means to open a url. At any rate, command line programs usually respond to command line parameters. Your program could open a url in the default browser in response to a command line flag. On Windows, you could call ShellExecute - on other systems, system might be appropriate.
It depends. In Windows, for example, yes, it's entirely possible, though somewhat non-trivial. You can receive mouse events via ReadConsoleInput, so in theory it's a fairly straightforward matter of reading the input event, and if it's a mouse click over the area you've defined as a link, you direct the user to the link as you see fit -- if you want to display the web site in text mode, that's possible (though, again, distinctly non-trivial). If you want to start up the user's normal web browser, that's a lot simpler (normally just ShellExecute the URL).
In reality, the details get a bit ugly. You have to enable mouse input for it to work at all. ReadConsoleInput gives you an INPUT_RECORD, which is a union of a number of different input record types, one of which is a mouse input record. By the time you get to react to a mouse click, your code is nested fairly deeply. None of it is unmanageable by any means, but unless you already have a fair amount of experience with Windows console programming, it might easily take most of a day (maybe even a bit more) before you have it working, rather than the hour or two you'd initially guess.
That, of course, is strictly for Windows -- if you ever want to port the code to another system, I'd guess there's a pretty good chance you'd be looking at a complete rewrite. For GUIs there are a fair number of cross-platform libraries, but text mode mouse operations aren't nearly so well supported.