Traal
asked on
Pressing ALT+A and having [ALT+A] show in EditBox (for starters).
I would like to be able to press any ALT or CTRL key combination in an EditBox, and have "[ALT+key]" or "[CTRL+key]" be displayed. Additionally, when backspacing in the EditBox, if the cursor is to the right of "]" when backspace is pressed, it should delete all the way to the preceeding "[". So, for example, if I had...
abcd[CTRL+S]efgh
...in an EditBox, and I placed the cursor immediately to the right of the "]" character and pressed the backspace key, the result should be "abcdefg". The corrolary to that is being to the left of the "[" character, pressing delete, and deleting all the way to the "]". (Help me with one, and I can extrapolate the other.)
Finally, and this is much less important than the previous two items, the cursor position should behave normally. That is, not jump to the beginning or end or the EditBox.
What I've done so far, with unsatisfactory results, is:
procedure TForm1.Edit1KeyDown(Sender : TObject; var Key: Word;
Shift: TShiftState);
var
i: Integer;
Temp: String;
begin
if (((ssAlt in Shift) or (ssCtrl in Shift)) and ((Key <> 16) and (Key <> 17) and (Key <> 18))) then
begin
if (ssAlt in Shift) then
Temp := 'ALT';
if (ssCtrl in Shift) then
begin
if (Temp <> '') then
Temp := Temp + '+';
Temp := Temp + 'CTRL';
end;
Edit1.Text := Edit1.Text + '[' + Temp + '+' + Chr(Key) + ']';
end;
if (Key = VK_BACK) then
begin
Key := 0;
if Copy(Edit1.Text, Edit1.SelStart, 1) = ']' then
begin
Temp := Edit1.Text;
for i := Edit1.SelStart downto 0 do
if Temp[i] = '[' then
begin
Delete(Temp, i, Edit1.SelStart - i);
break;
end;
Edit1.Text := Temp;
end;
end;
if (Key <> VK_LEFT) and
(Key <> VK_RIGHT) and
(Key <> VK_UP) and
(Key <> VK_DOWN) and
(Key <> VK_TAB) and
(Key <> VK_DELETE) then
Edit1.SelStart := Length(Edit1.Text);
end;
Brian
abcd[CTRL+S]efgh
...in an EditBox, and I placed the cursor immediately to the right of the "]" character and pressed the backspace key, the result should be "abcdefg". The corrolary to that is being to the left of the "[" character, pressing delete, and deleting all the way to the "]". (Help me with one, and I can extrapolate the other.)
Finally, and this is much less important than the previous two items, the cursor position should behave normally. That is, not jump to the beginning or end or the EditBox.
What I've done so far, with unsatisfactory results, is:
procedure TForm1.Edit1KeyDown(Sender
Shift: TShiftState);
var
i: Integer;
Temp: String;
begin
if (((ssAlt in Shift) or (ssCtrl in Shift)) and ((Key <> 16) and (Key <> 17) and (Key <> 18))) then
begin
if (ssAlt in Shift) then
Temp := 'ALT';
if (ssCtrl in Shift) then
begin
if (Temp <> '') then
Temp := Temp + '+';
Temp := Temp + 'CTRL';
end;
Edit1.Text := Edit1.Text + '[' + Temp + '+' + Chr(Key) + ']';
end;
if (Key = VK_BACK) then
begin
Key := 0;
if Copy(Edit1.Text, Edit1.SelStart, 1) = ']' then
begin
Temp := Edit1.Text;
for i := Edit1.SelStart downto 0 do
if Temp[i] = '[' then
begin
Delete(Temp, i, Edit1.SelStart - i);
break;
end;
Edit1.Text := Temp;
end;
end;
if (Key <> VK_LEFT) and
(Key <> VK_RIGHT) and
(Key <> VK_UP) and
(Key <> VK_DOWN) and
(Key <> VK_TAB) and
(Key <> VK_DELETE) then
Edit1.SelStart := Length(Edit1.Text);
end;
Brian
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Wow! That was quick, and right on the money, too.
I have added the following functionality in the code below:
*When using the right and left arrows, the cursor will skip past whatever is enclosed in [brackets].
*If you click in between [brackets] the cursor will be placed either immediately before the [ or immediately after the ], whichever is closer.
Thanks, TheRealLoki
Brian
// ************************** ********** ********** ********** ********** ***
procedure TForm1.Edit1KeyDown(Sender : TObject; var Key: Word;
Shift: TShiftState);
var
i, j: Integer;
Temp: String;
StringToInsert: string;
begin
if (((ssAlt in Shift) or (ssCtrl in Shift)) and ((Key <> 16) and (Key <> 17) and (Key <> 18))) then
begin
if (ssAlt in Shift) then
StringToInsert := 'ALT';
if (ssCtrl in Shift) then
begin
if (StringToInsert <> '') then
StringToInsert := StringToInsert + '+';
StringToInsert := StringToInsert + 'CTRL';
end;
StringToInsert := '[' + StringToInsert + '+' + Chr(Key) + ']';
if (Edit1.SelStart < Length(Edit1.Text)) then
begin
j := Edit1.SelStart;
Temp := Edit1.Text;
Insert(StringToInsert, Temp, j + 1);
Edit1.Text := Temp;
Edit1.SelStart := j + Length(StringToInsert);
end
else
begin
Edit1.Text := Edit1.Text + StringToInsert;
Edit1.SelStart := Length(Edit1.Text);
end;
Key := 0;
end;
if (Key = VK_BACK) then
begin
Key := 0;
if (Copy(Edit1.Text, Edit1.SelStart, 1) = ']') then
begin
Temp := Edit1.Text;
for i := Edit1.SelStart downto 1 do
if (Temp[i] = '[') then
begin
Delete(Temp, i, Edit1.SelStart - i);
break;
end;
Edit1.Text := Temp;
edit1.SelStart := i;
end;
end;
if (Key = VK_LEFT) then
begin
if (Copy(Edit1.Text, Edit1.SelStart, 1) = ']') then
begin
Temp := Edit1.Text;
for i := Edit1.SelStart downto 1 do
if (Temp[i] = '[') then
begin
Edit1.SelStart := i;
break;
end;
end;
end;
if (Key = VK_RIGHT) then
begin
j := Edit1.SelStart;
if j <= Length(Edit1.Text) then
begin
if (Copy(Edit1.Text, j + 1, 1) = '[') then
begin
Temp := Edit1.Text;
for i := j + 1 to Length(Edit1.Text) do
if (Temp[i] = ']') then
break;
Edit1.SelStart := i - 1;
end;
end;
end;
if (Key = VK_DELETE) then
begin
j := Edit1.SelStart;
if (j <= Length(Edit1.Text)) then
begin
if (Copy(Edit1.Text, j + 1, 1) = '[') then
begin
Key := 0;
Temp := Edit1.Text;
for i := j + 1 to Length(Edit1.Text) do
if (Temp[i] = ']') then
begin
Delete(Temp, j + 1, i-j);
break;
end;
Edit1.Text := Temp;
Edit1.SelStart := j;
end;
end;
end;
end; // Edit1KeyDown
// ************************** ********** ********** ********** ********** ***
procedure TForm1.Edit1Click(Sender: TObject);
var
i, j: Integer;
begin
if ((Edit1.SelStart > 0) and (Edit1.SelStart < Length(Edit1.Text))) then
begin
for i := Edit1.SelStart downto 0 do
if (Edit1.Text[i] = ']') then
break
else if (Edit1.Text[i] = '[') then
begin
for j := Edit1.SelStart to Length(Edit1.Text) do
if (Edit1.Text[j] = ']') then
begin
if ((Edit1.SelStart - (i - 1)) < (j - Edit1.SelStart)) then
Edit1.SelStart := i - 1
else
Edit1.SelStart := j;
exit;
end;
Edit1.SelStart := i - 1;
end;
end;
end; // Edit1Click
I have added the following functionality in the code below:
*When using the right and left arrows, the cursor will skip past whatever is enclosed in [brackets].
*If you click in between [brackets] the cursor will be placed either immediately before the [ or immediately after the ], whichever is closer.
Thanks, TheRealLoki
Brian
// **************************
procedure TForm1.Edit1KeyDown(Sender
Shift: TShiftState);
var
i, j: Integer;
Temp: String;
StringToInsert: string;
begin
if (((ssAlt in Shift) or (ssCtrl in Shift)) and ((Key <> 16) and (Key <> 17) and (Key <> 18))) then
begin
if (ssAlt in Shift) then
StringToInsert := 'ALT';
if (ssCtrl in Shift) then
begin
if (StringToInsert <> '') then
StringToInsert := StringToInsert + '+';
StringToInsert := StringToInsert + 'CTRL';
end;
StringToInsert := '[' + StringToInsert + '+' + Chr(Key) + ']';
if (Edit1.SelStart < Length(Edit1.Text)) then
begin
j := Edit1.SelStart;
Temp := Edit1.Text;
Insert(StringToInsert, Temp, j + 1);
Edit1.Text := Temp;
Edit1.SelStart := j + Length(StringToInsert);
end
else
begin
Edit1.Text := Edit1.Text + StringToInsert;
Edit1.SelStart := Length(Edit1.Text);
end;
Key := 0;
end;
if (Key = VK_BACK) then
begin
Key := 0;
if (Copy(Edit1.Text, Edit1.SelStart, 1) = ']') then
begin
Temp := Edit1.Text;
for i := Edit1.SelStart downto 1 do
if (Temp[i] = '[') then
begin
Delete(Temp, i, Edit1.SelStart - i);
break;
end;
Edit1.Text := Temp;
edit1.SelStart := i;
end;
end;
if (Key = VK_LEFT) then
begin
if (Copy(Edit1.Text, Edit1.SelStart, 1) = ']') then
begin
Temp := Edit1.Text;
for i := Edit1.SelStart downto 1 do
if (Temp[i] = '[') then
begin
Edit1.SelStart := i;
break;
end;
end;
end;
if (Key = VK_RIGHT) then
begin
j := Edit1.SelStart;
if j <= Length(Edit1.Text) then
begin
if (Copy(Edit1.Text, j + 1, 1) = '[') then
begin
Temp := Edit1.Text;
for i := j + 1 to Length(Edit1.Text) do
if (Temp[i] = ']') then
break;
Edit1.SelStart := i - 1;
end;
end;
end;
if (Key = VK_DELETE) then
begin
j := Edit1.SelStart;
if (j <= Length(Edit1.Text)) then
begin
if (Copy(Edit1.Text, j + 1, 1) = '[') then
begin
Key := 0;
Temp := Edit1.Text;
for i := j + 1 to Length(Edit1.Text) do
if (Temp[i] = ']') then
begin
Delete(Temp, j + 1, i-j);
break;
end;
Edit1.Text := Temp;
Edit1.SelStart := j;
end;
end;
end;
end; // Edit1KeyDown
// **************************
procedure TForm1.Edit1Click(Sender: TObject);
var
i, j: Integer;
begin
if ((Edit1.SelStart > 0) and (Edit1.SelStart < Length(Edit1.Text))) then
begin
for i := Edit1.SelStart downto 0 do
if (Edit1.Text[i] = ']') then
break
else if (Edit1.Text[i] = '[') then
begin
for j := Edit1.SelStart to Length(Edit1.Text) do
if (Edit1.Text[j] = ']') then
begin
if ((Edit1.SelStart - (i - 1)) < (j - Edit1.SelStart)) then
Edit1.SelStart := i - 1
else
Edit1.SelStart := j;
exit;
end;
Edit1.SelStart := i - 1;
end;
end;
end; // Edit1Click
ASKER
Hmmm. For some reason
if (Chr(Key) in ['[',']'])...
isn't firing on the [ or ] keys. Further examination shows that IntToStr(Ord('[')) produces 91, whereas IntToStr(Key) gives 219 when the key pressed was [. Interesting. Any thoughts?
Brian
if (Chr(Key) in ['[',']'])...
isn't firing on the [ or ] keys. Further examination shows that IntToStr(Ord('[')) produces 91, whereas IntToStr(Key) gives 219 when the key pressed was [. Interesting. Any thoughts?
Brian
you are using the KeyDown event, which includes the last bit for key: pressed or released
so you could do
(key and $7F) when comparing to eliminate the last bit
so you could do
(key and $7F) when comparing to eliminate the last bit
I changed your for loop to be "downto 1" instead of downto 0 because if there was only "alt+a]" with no preceding "[" then you could get an access violation.
You might want to think about disallowing "[" and "]" keys
e.g.
if chr(key) in ['[',']'] then key := 0;