Author Topic: Testing ideas for the hotkeys editor  (Read 2583 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Testing ideas for the hotkeys editor
« on: July 30, 2010, 05:25:32 AM »
Testing ideas for the hotkeys editor.

With the theory that with code skill you either "use it or lose it" I've been trying to do some study and KeyThumping at night.

This is a test bed for determining the keyValues of keys and key-combinations that we want to define in the new HotKeys Editor

The theory is that no keys generating printable characters are to be trapped .
Key modifiers can be [CTRL, ALT, SHIFT]
The  F1 -> F15  keys and  PDUP, PGDN, END, HOME, LEFT, UP, RIGHT, DOWN, INSERT, DELETE can be trapped without modifiers.
Other keys except
             (keyValue == 8 ) // Backspace
                    | (keyValue == 9) // Tab
                    | (keyValue == 13) // Enter
                    | (keyValue == 19) // Pause/Break
                    | (keyValue == 20) // CapLock
                    | (keyValue == 27) // ESC
                    | (keyValue == 144) // NumLock
                    | (keyValue == 145) // ScrLk
             )
can be trapped with a modifier.

The test app is built with NET 3.5 and the EXE and source are attached.
There are 6 forms each illustrating a different procedure for determining key values.
Form 6 demonstrates the procedure I think may work best.

Note that this is just for trapping the key values in the editor so thay a command string can ce assigned to the key combination and the definitiond writted to an XML fike for reading back into AutoCAD

Here's Form6 code, a piccy, the EXE and the source.
I'd be happy if someone tried to break it and/or critique the code ( keeping in mind this is first generation code )
Code - C#: [Select]
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace KeyEvents02
  11. {
  12.     public partial class Form6 : Form
  13.     {
  14.         public Form6()
  15.         {
  16.             InitializeComponent();
  17.  
  18.             this.txt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_KeyDownTranslator);
  19.             this.txt.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt_KeyPress);
  20.             txt.Focus();
  21.         }
  22.  
  23.         // StringBuilder strbMsg = new StringBuilder();
  24.         string stringFormat = "{0,-12} = {1}\r\n";
  25.  
  26.         private int m_actualKey = 0;
  27.         public int ActualKey
  28.         {
  29.             get { return m_actualKey; }
  30.             set { m_actualKey = value; }
  31.         }
  32.  
  33.         private string m_theKeys = string.Empty;
  34.         public string TheKeys
  35.         {
  36.             get { return m_theKeys; }
  37.             set { m_theKeys = value; }
  38.         }              
  39.  
  40.         private void txt_KeyDownTranslator(object sender, KeyEventArgs e)
  41.         {
  42.             // strbMsg.Clear(); //not available in .NET 3.5, so make the strbMsg local
  43.             StringBuilder strbMsg = new StringBuilder();
  44.             txt.Text = "";
  45.            
  46.             /*
  47.              *  If NO Modifier [CTRL, ALT, SHIFT] is active accept ONLY the  F1 -> F15  keys
  48.              *  or the non-printable keys :
  49.              *  PDUP, PGDN, END, HOME, LEFT, UP, RIGHT, DOWN, INSERT, DELETE
  50.              *  
  51.              *  */
  52.             KeysConverter kc = new KeysConverter();
  53.             int keyValue = e.KeyValue;
  54.             long modifierKey = 0;
  55.             ActualKey = 0;
  56.             TheKeys = string.Empty;
  57.  
  58.             //
  59.             // if the last key was a modifier, do nothing 'cause we don't know the actual key yet.
  60.             if ((keyValue == 16) //Shift
  61.                 | (keyValue == 17) // Control
  62.                 | (keyValue == 18) // Alt
  63.                 )
  64.             {
  65.                 // MessageBox.Show(keyValue.ToString(), "MODIFIER KEY");
  66.                 return;
  67.             }
  68.             //
  69.             // If a Modifier has been defined
  70.             // test if the current keyCode is suitable
  71.             else if (e.Control | e.Alt | e.Shift)
  72.             {
  73.                 if (
  74.                     (keyValue == 8) // Backspace
  75.                     | (keyValue == 9) // Tab
  76.                     | (keyValue == 13) // Enter
  77.                     | (keyValue == 19) // Pause/Break
  78.                     | (keyValue == 20) // CapLock
  79.                     | (keyValue == 27) // ESC
  80.                     | (keyValue == 144) // NumLock
  81.                     | (keyValue == 145) // ScrLk
  82.                     )
  83.                 {
  84.                     MessageBox.Show(e.Modifiers.ToString()
  85.                                     + "\n"
  86.                                     + kc.ConvertToString(e.KeyCode)
  87.                                     , "INVALID KEY");
  88.                     return;
  89.                 }
  90.                 //
  91.                 // A Modifier and actual key are valid, so assign the values.
  92.                 //
  93.                 else
  94.                 {
  95.                     modifierKey = (e.Alt ? 1 : 0)
  96.                                 + (e.Control ? 2 : 0)
  97.                                 + (e.Shift ? 4 : 0);
  98.                     ActualKey = keyValue;
  99.                     TheKeys = (e.Control ? "CTRL+" : string.Empty)
  100.                             + (e.Alt ? "ALT+" : string.Empty)
  101.                             + (e.Shift ? "SHIFT+" : string.Empty)
  102.                             + e.KeyCode;
  103.                                 }
  104.             }
  105.             //
  106.             // We know the keyValue is not a modifier
  107.             // and we know there has been no modifier defined, so
  108.             // Test if the keyValue is F1-> F15 or
  109.             // PDUP, PGDN, END, HOME, LEFT, UP, RIGHT, DOWN, INSERT, DELETE
  110.             //
  111.             else if (
  112.                         ((keyValue >= 33) & (keyValue <= 46))
  113.                         | ((keyValue >= 112) & (keyValue <= 126))
  114.                     )
  115.             {
  116.                 //MessageBox.Show(kc.ConvertToString(e.KeyCode)
  117.                 //                , "UNMODIFIED KEY");
  118.                 modifierKey = 0;
  119.                 ActualKey = keyValue;
  120.                 TheKeys = e.KeyData.ToString();
  121.             }
  122.             else
  123.             {
  124.                 // no valid option, so return
  125.                 return;
  126.             }
  127.             txt.Text = TheKeys;
  128.  
  129.             strbMsg.AppendFormat(stringFormat, "KeyCode", kc.ConvertToString(e.KeyCode));
  130.             strbMsg.AppendFormat(stringFormat, "Converted", TheKeys);
  131.  
  132.             strbMsg.AppendFormat(stringFormat, "modifierKey", modifierKey);
  133.             strbMsg.AppendFormat(stringFormat, "actualKey", ActualKey);
  134.  
  135.             strbMsg.AppendLine("//--------------------");
  136.             txtOutput.AppendText(strbMsg.ToString());
  137.         }
  138.  
  139.  
  140.         // This event occurs after the KeyDown event and can be used to prevent
  141.         // characters from entering the control.
  142.         private void txt_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
  143.         {
  144.             // Check for the KeyValue being set in the KeyDown event.
  145.             if (TheKeys == string.Empty)
  146.             {
  147.                 // Stop the character from being entered into the control since it is not a suitable value.
  148.                 e.Handled = true;
  149.             }
  150.         }
  151.  
  152.  
  153.         private void ClearInput_Click(object sender, EventArgs e)
  154.         {
  155.             txt.Clear();
  156.             txt.Focus();
  157.         }
  158.  
  159.         private void ClearOutput_Click(object sender, EventArgs e)
  160.         {
  161.             txtOutput.Clear();
  162.             txt.Focus();
  163.         }
  164.     }
  165. }
  166.  
  167.  
  168.  


« Last Edit: August 04, 2012, 07:01:25 PM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

pkohut

  • Guest
Re: Testing ideas for the hotkeys editor
« Reply #1 on: July 30, 2010, 05:44:39 AM »
Quick test with Launchy running, works fine. Launchy is getting launch key combo first and eating it (not passing it along). Same with LWin + Space (Windows 7).

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Testing ideas for the hotkeys editor
« Reply #2 on: July 30, 2010, 05:54:49 AM »

Thanks for playing Paul.
I haven't tried Launchy.

Can you clarify the eating bit .. and the LWin comment.  is that specific to Launchy ??

... I'm making dinner and have had a couple of glasses of red ( one for the sauce, one for me, repeat, repeat ... ) ; so my faculties are getting a little blurred.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

pkohut

  • Guest
Re: Testing ideas for the hotkeys editor
« Reply #3 on: July 30, 2010, 06:04:18 AM »

Thanks for playing Paul.
I haven't tried Launchy.

Can you clarify the eating bit .. and the LWin comment.  is that specific to Launchy ??

... I'm making dinner and have had a couple of glasses of red ( one for the sauce, one for me, repeat, repeat ... ) ; so my faculties are getting a little blurred.

LWin is the left windows key between Ctrl and Alt. LWin + Space on my machine give a quick view of the desktop (special name for it, but forget).

Launchy is an application launcher, and I've got it set to Alt + Space. So when the combo is hit a launchy window appears and gets keyboard focus. Since Launchy gets keyboard priority before other applications, ones like yours play nice with it, as expected but wanted to test.  :-) 

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Testing ideas for the hotkeys editor
« Reply #4 on: July 30, 2010, 06:14:22 AM »

Thanks Paul.

I should exclude the LWin and RWin and Space from selection anyway
 ... may also be others I missed ... haven't tried ALT F4
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Testing ideas for the hotkeys editor
« Reply #5 on: August 01, 2010, 12:45:20 AM »
The KeyEventArgs ..::. SuppressKeyPress Property has some interesting possibilities.

Piccy, code and EXE attached :-

Code - C#: [Select]
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10.  
  11. namespace KeyEvents02
  12. {
  13.     public partial class Form7 : Form
  14.     {
  15.         public Form7()
  16.         {
  17.             InitializeComponent();
  18.  
  19.             this.textBox1.KeyDown +=
  20.                 new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
  21.             this.textBox1.KeyPress +=
  22.                 new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
  23.  
  24.         }
  25.         // check later
  26.         // http://msdn.microsoft.com/en-us/library/system.windows.forms.keypresseventargs.aspx
  27.  
  28.         private void textBox1_KeyDown(object sender, KeyEventArgs e)
  29.         {
  30.             if (e.KeyCode.ToString() != "S")
  31.             {
  32.                 e.SuppressKeyPress = true;
  33.             }
  34.         }
  35.  
  36.         private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  37.         {
  38.             MessageBox.Show("In textBox1_KeyPress Event Handler"
  39.                 + "\nSo \"S\" has been pressed."
  40.                 + "\n\nWELL DONE !!"
  41.                 + "\n\nDo you want to play again?"                
  42.                 );
  43.         }
  44.  
  45.     }
  46. }
  47.  

« Last Edit: August 03, 2012, 02:39:37 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.