?
Solved

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

Posted on 2005-03-30
5
Medium Priority
?
305 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

762 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