Решил сделать прогу, которая при вводе строки позволяет использовать клавиши backspace, del и передвигаться по символам используя кнопки влево, вправо имея при этом возможность вставить символ в любое место. Ну что - то напутал, так как del и вставление символа не пашут. Наверно что - то с переменной Active, вгляните пожалуйста.
uses crt;
var Active : Integer; {позиция курсора}
s : string; {рабочая строка}
ch : char; {для посимвольной считки}
begin
writeln ('Vvedite stroku');
S:='' ;
Active:=1; {1, ведь изначально курсор после символа}
repeat
Clrscr;
Write (s);
read (ch);
if (ord(ch) = 8) and (Length(S) <> 0) then {BACKSPASE}
begin
gotoXY(WhereX - 1, Wherey);
Active:=Active - 1;
Delete (s,Active,Length(s));
end else
if (ord(ch) = 75) and (Length(S) <> 0 ) then {LEFT}
begin
gotoXY(whereX - 1, WhereY);
Active:=Active - 1;
end
else
if ord(ch) = 77 then {RIGHT}
begin
gotoXY(wherex + 1, Wherey);
Active:=Active + 1;
end
else
If (Active <= Length(s)) and (ch In ['a'..'z']) then Insert (ch,s,active) {INSERT}
else
if (Active <=Length(s)) and (ord(ch) = 83) then Delete (s,Active,1); {DELETE}
if Active = Length(s) then S:= S + Ch;
until ord(ch) = 13;
end.
uses crt;
function get_str(max_len: byte): string;
var s: string; ch: char;
begin
s := '';
repeat
ch := readkey;
if length(s) <= max_len then
case ch of
#8: begin
if length(s) > 0 then begin
write(#8,#32,#8);
delete(s, length(s), 1)
end
end
#13:;
else
if length(s) <> max_len then
begin s := s + ch; write(ch) end
end;
until ch = #13;
writeln;
get_str := s
end;
спасибо, но в этом алго помоему только бекспейс работает. Мне всё таки очень интересно разобраться со своими набросками.
Во-первых, посмотри, насколько проще становится структура программы при использовании Case:
uses crt;
var
Active: Integer;
s: string;
ch: char;
begin
writeln('Vvedite stroku');
s := '';
Active := 1;
repeat
gotoxy(1, WhereY); clreol;
write(s); gotoxy(Active, 1);
ch := readkey;
case ch of
#0:
case readkey of
#75:
if Active > 1 then begin { LEFT }
dec(Active);
gotoXY(Active, WhereY);
end;
#77:
if WhereX <= length(s) then begin { RIGHT }
inc(Active);
gotoXY(Active, Wherey);
end;
#83:
if Active <= Length(s) then
Delete(s, Active, 1); { DELETE }
end;
#8:
if length(s) <> 0 then begin { BS }
dec(Active);
gotoXY(Active, Wherey);
Delete(s, Active, length(s));
end;
#13:
break;
'a' .. 'z':
begin
if Active <= Length(s) then Insert(ch, s, active)
else s := s + ch;
inc(Active);
end;
end;
until false;
writeln;
writeln('s = ', s);
end.
Var s: string;. Но вот когда ты нажимаешь Enter, в Ch заносится только первый символ введенной тобой строки... Очень серьезная ошибка.
...
ReadLn(s);
Большое спасибо ! Действительна структура очень простая стала!