C++ linux read continuously string and detect backspace key - c++

i would like to know if there is a way to read a string continuously on the console and detect the backspace key if the user erase a character. Let me explain a little bit more, for example:
Write a word on the console: "Word" So i got the string. And i can use all the methods from string like access to the characters item[0] = "W" or item.length()
The user press the backspace key and know my string item will have "Wor"
If the user put another letter the string is updated "Worrtw"
I using this for recommend words according what user is typing. I posting this beacause i really don't found anything usefull at the moment for the keywords or something about the string.

Related

Using multi key sequenced inputs to result in a function

Straight to the point. I've been using visual studio to run a game I've been making. So far while coding in C++ I've been using inputs like VK_BACK or 0x52 for single key inputs. But I currently need to be able to type a word while the game is running to trigger a function. Basically, I'm asking how you would go about making the program recognize an inputted word. Like pressing the keys in a sequence to trigger a function.
Thanks, Jack
You've left a lot up to us to guess:
What are you using to read keyboard input?
I assume you're polling rather than streaming?
I'm also assuming non-blocking input?
With so many unknowns it's difficult to give a useful answer to the question. But here are a handful of options that may be helpful to you:
Require a terminating character for all keyboard input. This is characteristic of blocking input. The input is only read when the terminating character is placed.
Require the holding of a mode key while inputting a string. For example if I press an 'a' the key's command is executed immediately, but if I'm currently holding down the left Ctrl key and I press an 'a', it is treated as being within a string until the Ctrl key is released.
Use delimiters. If the '\'' is pressed it denoted the beginning of a string input consisting of all characters till the next '\'' key is pressed.
Make string entry keys separate from instantaneous input keys. This may be the most convenient solution because it allows instantaneous input in the middle of string input. But it may also be frustrating to the user if the allowable string commands are not clearly defined.

How to write a QRegExp that will accept only strings made of characters or normal symbols?

Let me clarify the question. I need to process keyboard input on the fly. So if the user presses something like the home button or the delete button I need to ignore the input. But any normal printable symbol I want to catch. I get the input in the text field() method of a QKeyEvent.
I've tried two regexps
The first one was:
QRegExp("[\\S]");
Which did not work as apparently pressing delete is considered a non-whitespace character.
The second one was
QRegExp("[\\w]");
This one was much better as it would only accept letters, number and underscore but no symbols such as + or *. Which I want to accept.
I was thinking of modifying the last expression to include any symbol in my keyboard. But how would I do that? Or how can I write an RegExp for what I want?

Go line up in windows console

It seems that you can go back one character from current line in the console using \b. However, the console doesn't seem to be able to jump one line up.
I want to mark invalid user input red. After typping input, user presses Enter which put's unerasable new line in the console.
My plan was to do the following:
Check the input for validity.
If it's invalid, print input.length()+1 times \b
Turn console color red
Print the input, print \n
But, the \b will not jump back to the line where user input is. So I have plan B:
Remember the length of string that was before user input (query_string)
Check the input for validity.
If it's invalid, go line up (where the input was entered)
Jump to query_string.length() character
Turn console color red
Print the input, print \n
However, I don't know how to do this using the console API.
There are at least two ways you can do this.
One way is, as #chris implied in a comment, is to save the cursor position of where the user started typing. When you find bad input, you set the cursor back to that position and change the text attribute of the characters he entered.
You probably don't want to scroll the window back up one line. If you do, then the window will appear to "jump" when the user makes an error. It's a really jarring user interface experience. But if you want to try, you can call ScrollConsoleScreenBuffer.
Another way to do it would be to change the console mode so that it doesn't automatically echo characters when the user types them. Instead, you read each character individually, append it to your input buffer, and when the user presses Enter you validate. If the input is valid, you issue a newline to move to the next line. Otherwise you back up and highlight the erroneous input. This sounds like a lot more work, but it's not that difficult and it results in a much better UI experience.

Dynamically check the input as the user types

In a C++ program I want to check the user input as he types. I want to get a string from the user, but I need to make sure the user enters not more than 25 words in the string. The input must automatically stop on the 25th word and the program must go ahead. Is there any function in C++ that can do this purpose. getch() can get a single character, but that is not what I want.

Converting a VK_CODE into a displayable string

When writing a Windows application, the documentation says that some VK_CODEs are displayable characters, like VK_OEM1 is "o with an umlaut". How can I go from the WPARAM of non-ASCII characters into a displayable string? I'm using UTF-16.
Maybe you're looking for the GetKeyNameText Function
It retrieves a string that represents the name of a key.
like VK_OEM1 is "o with an umlaut".
Maybe on your machine. Not on mine, it is ';' or ':', depending on the Shift key state. These are virtual key codes. The ones that represent a typing key get converted to a character by ToUnicodeEx(), a function that takes a keyboard layout. And of course you have the non-typing keys that produce no character at all, like VK_F1 or VK_NUMLOCK. This gets a lot more complicated when the keyboard layout has dead keys, the kind you use to get a diacritic on top of a character. That why the function also requires a keyboard state.
Avoid this like the plague, WM_CHAR is your friend.