Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

1

06.05.2007, 12:54

großes problem mit eingaberoutine

Hallo,
ich habe einen Zeiger (char *playername)!
Wenn ich jetzt die Taste 'A' drücke, soll *playername gleich "A" sein.
Wenn ich jetzt noch 'B' drücke, soll *playername gleich "AB" sein.

und wenn ich die BackSpace drücke, soll das letzte Zeichen gelöscht werden.

wie bekomme ich das hin?

grek40

Alter Hase

Beiträge: 1 491

Wohnort: Dresden

  • Private Nachricht senden

2

06.05.2007, 13:48

Ich hab mal was in der Richtung für Console gemacht... ob es sinnvoll ist oder nicht, darüber kann man sich sicher streiten^^

Mit Capslock das klappt übrigens noch nicht so richtig :roll:
Was deiner Frage entspricht ist _Keycontrol::Instance().GetString(dein_std::string)

Input.h

C-/C++-Quelltext

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
namespace Input
{
    class _Keycontrol
    {
    private:
        _Keycontrol():_Timer(0), _LastTimer(0), _TimeDiff(0)
        {
            std::locale::global(std::locale("German_germany"));
            memset(_Keys, 0, sizeof (_Keys));
            memset(_Oldkeys, 0, sizeof (_Oldkeys));
            memset(_TimeElapsed, 0, sizeof (_TimeElapsed));
        }
        _Keycontrol(const _Keycontrol& _copy)
        {}
        ~_Keycontrol()
        {}
        const _Keycontrol& operator = (const _Keycontrol& _copy)
        {return _copy;}
        bool _isActive() const
        {return (GetForegroundWindow()==GetConsoleWindow());}

    public:
        static _Keycontrol& Instance()
        {
            static _Keycontrol _instance;
            return _instance;
        }

        void Update();
        bool KeyPressed(unsigned long vKey) const;
        bool KeyReleased(unsigned long vKey) const;
        bool KeyDown(unsigned long vKey) const;
        bool KeyDownDelayed(unsigned long vKey);
        void GetString(std::basic_string<_TCHAR>& out);

    private:
        bool _Oldkeys[256];
        bool _Keys[256];
        unsigned long _TimeElapsed[256];
        unsigned long _Timer, _LastTimer, _TimeDiff;
    };
};


Input.cpp

C-/C++-Quelltext

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
namespace Input
{
    void _Keycontrol::Update()
    {
        _LastTimer=_Timer;
        _Timer=timeGetTime();
        _TimeDiff=_Timer-_LastTimer;
        if ( memcpy_s( _Oldkeys, sizeof(_Oldkeys), _Keys, sizeof(_Keys)))
            EXCEPTION(Exception, _T("memcpy_s fehlgeschlagen"));
        for (unsigned long i=0; i<256; ++i)
        {
            _Keys[i] = (GetAsyncKeyState(i) & 0x8000)!=0;
            if (_Keys[i] && !_Oldkeys[i])
                _TimeElapsed[i]=100;
            else if (_Keys[i])
                _TimeElapsed[i] += _TimeDiff;
        }
    }

    bool _Keycontrol::KeyPressed(unsigned long vKey) const
    {
        if (!_isActive())
            return false;
        return (_Keys[vKey] && !(_Oldkeys[vKey]));
    }

    bool _Keycontrol::KeyReleased(unsigned long vKey) const
    {
        STACK;
        if (!_isActive())
            return false;
        return (!(_Keys[vKey]) && _Oldkeys[vKey]);
    }

    bool _Keycontrol::KeyDown(unsigned long vKey) const
    {
        if (!_isActive())
            return false;
        return (_Keys[vKey]);
    }

    bool _Keycontrol::KeyDownDelayed(unsigned long vKey)
    {
        if (!_isActive())
            return false;
        if (_Keys[vKey] && (_TimeElapsed[vKey] >=100))
        {
            _TimeElapsed[vKey]=0;
            return true;
        }
        return false;
    }

    void _Keycontrol::GetString(std::basic_string<_TCHAR>& out)
    {
        while (!_isActive())
            Sleep(0);
        out.clear();
        unsigned long written=0;
        CONSOLE_SCREEN_BUFFER_INFO _info;
        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &_info);
        Update();
        while (!KeyPressed(VK_RETURN))
        {
            unsigned short specialKeys=0;
            if (KeyDown(VK_SHIFT))
                specialKeys |= 0x0001;
            if (KeyDown(VK_CONTROL))
                specialKeys |= 0x0002;
            if (KeyDown(VK_MENU))
                specialKeys |= 0x0004;
            if (KeyDown(VK_INSERT))
                specialKeys |= 0x0008;
            if (GetAsyncKeyState(VK_CAPITAL))
                specialKeys |= 0x0010;
            if (KeyPressed(VK_BACK) || KeyPressed(VK_DELETE))
            {
                if (!out.empty())
                    out.erase(out.size()-1);
            }
            else
            {
                if (KeyPressed(L'2') && (specialKeys & 0x0006))         out += _T('²');
                else if (KeyPressed(L'3') && (specialKeys & 0x0006))    out += _T('³');
                else if (KeyPressed(L'7') && (specialKeys & 0x0006))    out += _T('{');
                else if (KeyPressed(L'8') && (specialKeys & 0x0006))    out += _T('[');
                else if (KeyPressed(L'9') && (specialKeys & 0x0006))    out += _T(']');
                else if (KeyPressed(L'0') && (specialKeys & 0x0006))    out += _T('}');
                else if (KeyPressed(0xDB) && (specialKeys & 0x0006))    out += _T('\\');
                else if (KeyPressed(L'Q') && (specialKeys & 0x0006))    out += _T('@');
                else if (KeyPressed(L'E') && (specialKeys & 0x0006))    out += _T('€');
                else if (KeyPressed(0xBB) && (specialKeys & 0x0006))    out += _T('~');
                else if (KeyPressed(0xE2) && (specialKeys & 0x0006))    out += _T('|');
                else if (KeyPressed(L'M') && (specialKeys & 0x0006))    out += _T('µ');
////////////////////////////////////////////////////////////////////

                else if (KeyPressed(0xDC) && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('°');
                else if (KeyPressed(L'1') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('!');
                else if (KeyPressed(L'2') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('"');
                else if (KeyPressed(L'3') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('§');
                else if (KeyPressed(L'4') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('$');
                else if (KeyPressed(L'5') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('%');
                else if (KeyPressed(L'6') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('&');
                else if (KeyPressed(L'7') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('/');
                else if (KeyPressed(L'8') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('(');
                else if (KeyPressed(L'9') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T(')');
                else if (KeyPressed(L'0') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('=');
                else if (KeyPressed(0xDB) && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('?');
                else if (KeyPressed(0xDD) && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('`');
                else if (KeyPressed(0xBF) && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('\'');
                else if (KeyPressed(L'Q') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('Q');
                else if (KeyPressed(L'W') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('W');
                else if (KeyPressed(L'E') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('E');
                else if (KeyPressed(L'R') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('R');
                else if (KeyPressed(L'T') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('T');
                else if (KeyPressed(L'Z') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('Z');
                else if (KeyPressed(L'U') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('U');
                else if (KeyPressed(L'I') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('I');
                else if (KeyPressed(L'O') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('O');
                else if (KeyPressed(L'P') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('P');
                else if (KeyPressed(0xBA) && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('Ü');
                else if (KeyPressed(0xBB) && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('*');
                else if (KeyPressed(L'A') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('A');
                else if (KeyPressed(L'S') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('S');
                else if (KeyPressed(L'D') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('D');
                else if (KeyPressed(L'F') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('F');
                else if (KeyPressed(L'G') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('G');
                else if (KeyPressed(L'H') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('H');
                else if (KeyPressed(L'J') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('J');
                else if (KeyPressed(L'K') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('K');
                else if (KeyPressed(L'L') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('L');
                else if (KeyPressed(0xC0) && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('Ö');
                else if (KeyPressed(0xDE) && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('Ä');
                else if (KeyPressed(0xE2) && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('>');
                else if (KeyPressed(L'Y') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('Y');
                else if (KeyPressed(L'X') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('X');
                else if (KeyPressed(L'C') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('C');
                else if (KeyPressed(L'V') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('V');
                else if (KeyPressed(L'B') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('B');
                else if (KeyPressed(L'N') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('N');
                else if (KeyPressed(L'M') && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('M');
                else if (KeyPressed(0xBC) && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T(';');
                else if (KeyPressed(0xBE) && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T(':');
                else if (KeyPressed(0xBD) && ((specialKeys & 0x81)!=0 && (specialKeys & 0x81)!=0x81))   out += _T('_');
////////////////////////////////////////////////////////////////////

                else if (KeyPressed(0xDC))  out += _T('^');
                else if (KeyPressed(L'1'))  out += _T('1');
                else if (KeyPressed(L'2'))  out += _T('2');
                else if (KeyPressed(L'3'))  out += _T('3');
                else if (KeyPressed(L'4'))  out += _T('4');
                else if (KeyPressed(L'5'))  out += _T('5');
                else if (KeyPressed(L'6'))  out += _T('6');
                else if (KeyPressed(L'7'))  out += _T('7');
                else if (KeyPressed(L'8'))  out += _T('8');
                else if (KeyPressed(L'9'))  out += _T('9');
                else if (KeyPressed(L'0'))  out += _T('0');
                else if (KeyPressed(0xDB))  out += _T('ß');
                else if (KeyPressed(0xDD))  out += _T('´');
                else if (KeyPressed(0xBF))  out += _T('#');
                else if (KeyPressed(0x09))  out += _T('\t');
                else if (KeyPressed(L'Q'))  out += _T('q');
                else if (KeyPressed(L'W'))  out += _T('w');
                else if (KeyPressed(L'E'))  out += _T('e');
                else if (KeyPressed(L'R'))  out += _T('r');
                else if (KeyPressed(L'T'))  out += _T('t');
                else if (KeyPressed(L'Z'))  out += _T('z');
                else if (KeyPressed(L'U'))  out += _T('u');
                else if (KeyPressed(L'I'))  out += _T('i');
                else if (KeyPressed(L'O'))  out += _T('o');
                else if (KeyPressed(L'P'))  out += _T('p');
                else if (KeyPressed(0xBA))  out += _T('ü');
                else if (KeyPressed(0xBB))  out += _T('+');
                else if (KeyPressed(L'A'))  out += _T('a');
                else if (KeyPressed(L'S'))  out += _T('s');
                else if (KeyPressed(L'D'))  out += _T('d');
                else if (KeyPressed(L'F'))  out += _T('f');
                else if (KeyPressed(L'G'))  out += _T('g');
                else if (KeyPressed(L'H'))  out += _T('h');
                else if (KeyPressed(L'J'))  out += _T('j');
                else if (KeyPressed(L'K'))  out += _T('k');
                else if (KeyPressed(L'L'))  out += _T('l');
                else if (KeyPressed(0xC0))  out += _T('ö');
                else if (KeyPressed(0xDE))  out += _T('ä');
                else if (KeyPressed(0xE2))  out += _T('<');
                else if (KeyPressed(L'Y'))  out += _T('y');
                else if (KeyPressed(L'X'))  out += _T('x');
                else if (KeyPressed(L'C'))  out += _T('c');
                else if (KeyPressed(L'V'))  out += _T('v');
                else if (KeyPressed(L'B'))  out += _T('b');
                else if (KeyPressed(L'N'))  out += _T('n');
                else if (KeyPressed(L'M'))  out += _T('m');
                else if (KeyPressed(0xBC))  out += _T(',');
                else if (KeyPressed(0xBE))  out += _T('.');
                else if (KeyPressed(0xBD))  out += _T('-');
                else if (KeyPressed(0x20))  out += _T(' ');
            }
#ifdef UNICODE
            std::wcout << Console::Pos(_info.dwCursorPosition.X, _info.dwCursorPosition.Y)
                << out.c_str() << _T("    ");
#else
            std::cout << Console::Pos(_info.dwCursorPosition.X, _info.dwCursorPosition.Y)
                << out.c_str() << _T("    ");
#endif
            while (!_isActive())
                Sleep(0);
            Update();
        }
    }
};