input with out the enter

i need to make a program that takes input but doesn't require you to press the ENTER key, is there any library or header files that has that function?
closed account (Dy7SLyTq)
are you on linux? if so i can give you a class that will accomplish this. other wise you need ncurses(for linux if you dont want my class) or on windows you need windows.h or conio.h i cant remember which. there is also pdcurses

edit: didnt realize what section im in
Last edited on
closed account (N36fSL3A)
Would you like a cross-platform or a windows-specific way? You could use conio's getch(); function, and use the VK_* macros to represent the keys.

Or you can use SDL or SFML's keyboard functions... they also allow you to create graphical apps later on.
This is a very common subject and question. I'll assume since you asked this question in a Windows specific forum you are only interested in Windows.

There are two general ways to go about it. The first way is to recognize that the Window Procedure for an edit control (if that's where you are receiving input) is within system code within Windows. That Window Procedure will receive all keypresses (including the [ENTER] key which you are interested in) in terms of WM_CHAR and WM_KEYDOWN messages. Again, these will occur in system code - not in your app. However, the SetWindowLong()/SetWindowLongPtr() functions allow you to hook into that internal Window Procedure and route all those internal messages to a Window Procedure you place within your application - where you have access to them. When you receive a VK_RETURN keypress you can then call SetFocus() to set the focus wherever you want. This technique goes by the name of Window Subclassing.

I said there were two ways to do it. Another way is to write your own custom "edit" control. Then the Window Procedure will be directly accessable in your app. You likely don't want to do this, so I mentioned it last.
Last edited on
i was hoping to use the windows.h library, do you know and youtube tuts
Here is a quick example I knocked up of how to do it in the console:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <windows.h>
#include <cstdio>
#include <cstdlib>

// Takes an array to store the data in, and the number of characters to store
void getInput(LPSTR& lpszInput, WORD wNumChars) {
    HANDLE hStdIn, hStdOut; // Console handles
    DWORD consoleMode; // Stores the old console mode

    LPSTR lpszPrompt = (LPSTR)"Type text: "; // text to display
    CHAR chBuffer[1]; // buffer for input, means one char at a time
    LPSTR chInput = new CHAR[wNumChars]; // temporary buffer
    DWORD cRead, cWritten; // number of characters read / written
    WORD wCharacters = 0; // number of characters read so far

    // Fill with null terminators in the case of a Ctrl+C to stop overflows
    ZeroMemory(chInput, wNumChars);    

    // Get the console handles
    hStdIn = GetStdHandle(STD_INPUT_HANDLE);
    hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

    // Write the prompt to the screen
    if (!WriteFile(hStdOut, lpszPrompt, lstrlenA(lpszPrompt),
                   &cWritten, NULL)) {
        MessageBox(NULL, "WriteFile", "Console Error",
                   MB_OK | MB_ICONERROR);
        exit(1);
    }

    // Get the current console mode, save it for later
    if (!GetConsoleMode(hStdIn, &consoleMode)) {
        MessageBox(NULL, "GetConsoleMode", "Console Error",
                   MB_OK | MB_ICONERROR);
        exit(1);
    }

    // Set out own console mode
    if (!SetConsoleMode(hStdIn, ENABLE_PROCESSED_INPUT)) {
        MessageBox(NULL, "SetConsoleMode", "Console Error",
                   MB_OK | MB_ICONERROR);
        exit(1);
    }

    // While there is room in the buffer and reading hasn't failed
    while (wCharacters < wNumChars &&
           ReadFile(hStdIn, chBuffer, 1, &cRead, NULL)) {
        // increment the number of characters read
        wCharacters += cRead;
        if (wCharacters == 0)
            continue;

        // We are using a buffer size 1, and newline is 2 chars ("\r\n"), so lets fix that
        if (*chBuffer == '\r') *chBuffer = '\n';

        // Copy the input into the temporary buffer
        chInput[wCharacters - 1] = *chBuffer;

        // Write what we input to the console (otherwise nothing appears)
        if (!WriteFile(hStdOut, chBuffer, cRead, &cWritten, NULL))
            break;
    }

    // copy our temporary buffer to the parameter passed
    strcpy(lpszInput, chInput);

    // reset the console mode to what it was before
    if (!SetConsoleMode(hStdIn, consoleMode)) {
        MessageBox(NULL, "SetConsoleMode", "Console Error",
                   MB_OK | MB_ICONERROR);
        exit(1);
    }
}

int main() {
    // example usage
    CHAR chText[10];
    getInput(chText, 10);

    printf(" : %s", chText);
    return EXIT_SUCCESS;
}


It currently has a few bugs, mostly to do with memory allocation and special character keys (like backspace). If you don't understand anything, MSDN is your best friend here. The way it works is you specify a pointer to an array of characters and the size of it, and it sits there reading characters until it fills up the buffer, and then copies it and terminates. Have fun!

EDIT:
Commented it

EDIT2:
You can also make it stop when there is a newline or a space or something just by putting a check in the while loop, too (just suddenly thought I should point that out).
Last edited on
Thank you,
popa6200
Topic archived. No new replies allowed.