[DEV] CustomRegions - OnKeyPress

[DEV] CustomRegions - OnKeyPress - Messages

#1 Posted: 2/7/2014 8:36:06 AM
Davide Carpi

Davide Carpi

1415 likes in 2872 posts.

Group: Moderator

I was wondering if it was possible to have the OnKeyPress event handler in custom regions... f.e. would be useful to filter the inputs in the region placeholder (the onkeydown doesn't allow to detect all the characters, like the left parenthesis)
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#2 Posted: 4/19/2014 9:11:54 AM
Вячеслав Мезенцев

Вячеслав Мезенцев

1402 likes in 1708 posts.

Group: Moderator

I join the request.
I'm trying to make an editor for scripts and I need more events:

OnKeyUp, OnKeyPress
OnClick, OnDoubleClick,
OnGotFocus, OnLostFocus

Now I have to somehow imitate them.
2014-04-19 19-01-56 SMath Studio Desktop - [Лист1 ].png
Russia ☭ forever, Viacheslav N. Mezentsev
1 users liked this post
Davide Carpi 4/19/2014 9:19:00 AM
#3 Posted: 4/19/2014 9:24:58 AM
Davide Carpi

Davide Carpi

1415 likes in 2872 posts.

Group: Moderator

Thank you Viacheslav, I'll add these requests in SS-72


Best wishses and Happy Easter,

Davide


P.S.: do you know something about these (incomplete?) interfaces: IRegionWithEditableText, IRegionWithInputData ?
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
1 users liked this post
#4 Posted: 4/19/2014 9:35:10 AM
Вячеслав Мезенцев

Вячеслав Мезенцев

1402 likes in 1708 posts.

Group: Moderator

Wrote

Thank you Viacheslav, I'll add these requests in SS-72

Best wishses and Happy Easter,
Davide

P.S.: do you know something about these (incomplete?) interfaces: IRegionWithEditableText, IRegionWithInputData ?


Unfortunately, no. But I can tell how to simulate OnKeyPress().
    public class KeyCodeToAscii {

	    [DllImport("User32.dll"]
	    public static extern int ToAscii(int uVirtKey, int uScanCode, byte[] lpbKeyState, byte[] lpChar, int uFlags);

	    [DllImport("User32.dll"]
	    public static extern int GetKeyboardState(byte[] pbKeyState);

	    public static char GetAsciiCharacter(int uVirtKey) {

		    var lpKeyState = new byte[256];
		    
            GetKeyboardState( lpKeyState );
		    
            var lpChar = new byte[2];

		    if ( ToAscii( uVirtKey, 0, lpKeyState, lpChar, 0 ) == 1) {
			    
                return Convert.ToChar( ( lpChar[0] ) );

		    } else {
			    
                return new char();
		    }
	    }

    }

and

public override void OnKeyDown( KeyEventArgs e ) {
    
    var keyChar = KeyCodeToAscii.GetAsciiCharacter( e.KeyValue );

    OnKeyPress( new KeyPressEventArgs( keyChar ) );
}

public void OnKeyPress( KeyPressEventArgs e ) {

    // ...
}
But it works only with english characters.
2014-04-19 19-27-00 SMath Studio Desktop - [Лист1 ].png
Russia ☭ forever, Viacheslav N. Mezentsev
1 users liked this post
Davide Carpi 4/19/2014 9:37:00 AM
#5 Posted: 5/2/2014 8:33:18 PM
Davide Carpi

Davide Carpi

1415 likes in 2872 posts.

Group: Moderator

Wrote

Unfortunately, no. But I can tell how to simulate OnKeyPress().


Really good hint... starting from here, I've deeply searched and in the end I've found this: http://stackoverflow.com/questions/370754/convert-a-keycode-to-the-relevant-display-character

Here a small adaptation:

using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;

namespace ComboBoxListRegion
{
	/// <summary>
	/// hook adapted from: http://stackoverflow.com/questions/370754/convert-a-keycode-to-the-relevant-display-character
	/// </summary>
	public class KeyboardHelper
	{
		[DllImport("user32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
        private static extern int ToUnicodeEx(
            uint wVirtKey,
            uint wScanCode,
            Keys[] lpKeyState,
            StringBuilder pwszBuff,
            int cchBuff,
            uint wFlags,
            IntPtr dwhkl);

        [DllImport("user32.dll", ExactSpelling = true)]
        internal static extern IntPtr GetKeyboardLayout(uint threadId);

        [DllImport("user32.dll", ExactSpelling = true)]
        internal static extern bool GetKeyboardState(Keys[] keyStates);

        [DllImport("user32.dll", ExactSpelling = true)]
        internal static extern uint GetWindowThreadProcessId(IntPtr hwindow, out uint processId);

        public static string CodeToString(int scanCode)
        {
            uint procId;
            uint thread = GetWindowThreadProcessId(Process.GetCurrentProcess().MainWindowHandle, out procId);
            IntPtr hkl = GetKeyboardLayout(thread);

            if (hkl == IntPtr.Zero)
            {
                MessageBox.Show("Sorry, that keyboard does not seem to be valid.";
                return string.Empty;
            }

            Keys[] keyStates = new Keys[256];
            if (!GetKeyboardState(keyStates))
                return string.Empty;

            StringBuilder sb = new StringBuilder(10);
            int rc = ToUnicodeEx((uint)scanCode, (uint)scanCode, keyStates, sb, sb.Capacity, 0, hkl);
            return sb.ToString();
        }
	}
}

and

public override void OnKeyDown( KeyEventArgs e ) {
    
	string keyChar = KeyboardHelper.CodeToString( e.KeyValue );
	
	if (keyChar != String.Empty)
		OnKeyPress( new KeyPressEventArgs( (char)keyChar[0] ) );
}

public void OnKeyPress( KeyPressEventArgs e ) {

    // ...
}

This works also with Cyrillic and Greek
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
1 users liked this post
#6 Posted: 5/3/2014 5:14:08 AM
Вячеслав Мезенцев

Вячеслав Мезенцев

1402 likes in 1708 posts.

Group: Moderator

Andrey gave me to test the latest build. There OnKeyPress event is present. You can ask him for more events. Unfortunately, many of the events do not come to the component. For example, the arrow keys.
Russia ☭ forever, Viacheslav N. Mezentsev
1 users liked this post
Davide Carpi 5/3/2014 5:28:00 AM
#7 Posted: 5/3/2014 5:21:08 AM
Davide Carpi

Davide Carpi

1415 likes in 2872 posts.

Group: Moderator

Wrote

Andrey gave me to test the latest build. There OnKeyPress event is present. You can ask him for more events.


Many thanks


Wrote

Unfortunately, many of the events do not come to the component. For example, the arrow keys.


We have the OnKeyDown event, so we can handle the arrows from here (or I'm missing something? )


public override void OnKeyDown(KeyEventArgs e)
{	
	if (!(this.ShowDescription && this.Description.Focused))
	{	
		if (e.KeyCode == Keys.Return)
		{
		  // ...
		}
		else if (e.KeyCode == Keys.Up)
		{
		  // ...
		}
		else if (e.KeyCode == Keys.Down)
		// ...
	}
	base.OnKeyDown(e);
}
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#8 Posted: 5/3/2014 5:31:00 AM
Вячеслав Мезенцев

Вячеслав Мезенцев

1402 likes in 1708 posts.

Group: Moderator

Events from these keys do not reach the handler within the plugin because they used to work with the regions. Andrey, I hope, will correct this.
Russia ☭ forever, Viacheslav N. Mezentsev
1 users liked this post
Davide Carpi 5/3/2014 5:32:00 AM
#9 Posted: 5/3/2014 5:41:19 AM
Davide Carpi

Davide Carpi

1415 likes in 2872 posts.

Group: Moderator

Wrote

I hope, will correct this.


Thanks Viacheslav I've missed this point. I hope the same
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
  • New Posts New Posts
  • No New Posts No New Posts