?
Solved

Pressing ALT+A and having [ALT+A] show in EditBox (for starters).

Posted on 2005-03-30
5
Medium Priority
?
312 Views
Last Modified: 2010-04-05
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
0
Comment
Question by:Traal
  • 2
  • 2
5 Comments
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 2000 total points
ID: 13667344
I think this is what you want

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_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;
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 13667419
oh, I also allowed you to _insert_ shortcuts anywhere  e.g. "somewhere" becomes "some[alt-a]where" if you press the keys while in the middl, and the cursor is still positioned just before the "where"
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;
0
 

Author Comment

by:Traal
ID: 13668553
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
0
 

Author Comment

by:Traal
ID: 13668622
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
0
 
LVL 13

Expert Comment

by:BlackTigerX
ID: 13671409
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
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month12 days, 19 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question