Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

Winforms shortcut key in X#? 17 Oct 2022 23:03 #24195

  • ic2
  • ic2's Avatar
  • Topic Author


  • Posts: 1574
  • I have this working in C#in an OnWindowKeyUp event (I think it should also work on a KeyDown event), in WinForms:
    if (e.Key == Key.Down && Keyboard.Modifiers == ModifierKeys.Control)
    // Ctrl cursor, do something

    This is the code ILSpy produces:
    if (e:Key == Key.Down) .AND. (Keyboard.Modifiers == ModifierKeys.Control)

    This gives no less dan 3 compiler errors. One is solved by adding Using Windows.System.Input but the XS0103 (The name 'KeyBoard' does not exist in the current context and XS1061 'System.Windows.Forms.KeyEventArgs' does not contain a definition for 'Key' and no accessible extension method 'Key' accepting a first argument of type 'System.Windows.Forms.KeyEventArgs' could be found (are you missing a using directive or an assembly reference?)

    remain. In C# the tooltip says Keyboard also using System.Windows.Input

    What should I change to catch a Ctrl/Alt/Shift + key in Winforms X#? And if the syntax from ILSpy is not correct, can anyone explain why not?

    Dick

    Please Log in or Create an account to join the conversation.

    Winforms shortcut key in X#? 17 Oct 2022 23:27 #24196

    • Fabrice
    • Fabrice's Avatar


  • Posts: 317
  • Hi Dick,
    May be my ILSpy-Plugin is wrong, but... the code you mention is not enough to reply.
    You are talking about WinForm but Windows.System.Input is for WPF.
    This may explain the missing Key definition.
    For WinForms, add a Using System.Windows.Forms.
    As I don't know exactly what you want to achieve, I suggest you have a look at the MS Documentation : learn.microsoft.com/en-us/dotnet/api/sys...w=netframework-4.7.2

    Fab

    Please Log in or Create an account to join the conversation.

    Winforms shortcut key in X#? 18 Oct 2022 00:48 #24197

    • Chris
    • Chris's Avatar


  • Posts: 3759
  • Hi Dick,

    As Fabrice said, this c# code is not WinForms code, it's WPF code.

    The equivalent WinForms code would be something like (after making sure you have a USING System.Windows.Forms in the top of your prg):

    IF e:KeyCode == Keys.Down .and. Control.ModifierKeys == Keys.Control

    Of course you also need to have a reference to the System.Windows.Forms library and remove any WPF references (libraries) may you have added.
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    Winforms shortcut key in X#? 18 Oct 2022 12:37 #24203

    • ic2
    • ic2's Avatar
    • Topic Author


  • Posts: 1574
  • Hello Chris, Fabrice,

    Ahhh, you are absolutely right. I was indeed looking in a solution where I have both WPF and Winfoms....and picked the WPF sample. Thanks!

    Fabrice, from you reply I wondered: do you have to provide "translation" for all language elements? That seems a bit of an impossible task. I thought that the IL would help find X# syntax using the compiler "reversed" , just like the compiler creates valid IL from X#.

    For future readers:

    If you click in an empty space of the WInforms window, select properties, use the lightning symbol, then go to Key, click on KeyDown, it will create an event called MyWindow_KeyDown.

    In the Misc properties, set KeyPreview to true

    Here's a sample which reacts on Alt + cursor left and Ctrl F:
    Private Method MyWindow_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) As Void Strict
    	If e:KeyCode == Keys.Left .and. Control.ModifierKeys == Keys.Alt
    		// Do something
    	Endif
    	If e:KeyCode == Keys.F .and. Control.ModifierKeys == Keys.Control
    		// Do something
    	Endif
    Return

    Dick

    Please Log in or Create an account to join the conversation.

    Winforms shortcut key in X#? 18 Oct 2022 14:59 #24207

    • Fabrice
    • Fabrice's Avatar


  • Posts: 317
  • Dick

    Fabrice, from you reply I wondered: do you have to provide "translation" for all language elements? That seems a bit of an impossible task. I thought that the IL would help find X# syntax using the compiler "reversed" , just like the compiler creates valid IL from X#.

    Mmm...that would be so simple...
    When ILSpy is decompile IL, it will call a kind of eventHandler for each structure (if, while, method call, parameters, ... ) which is a leaf in the compiled-tree.
    So I have to catch each one, and rewrite it the X# way.
    For Example, I'm doing two passes to enumerate all LOCALS, then inject their declaration in the second pass and provide all LOCALS at the beginning of code, etc...
    So sometimes, the generated code is not perfect because I just write what is arriving from ILSpy, and looks like C# mostly.
    But any sample to have a better translation is welcome ! :)

    Fab

    Please Log in or Create an account to join the conversation.

    Winforms shortcut key in X#? 19 Oct 2022 14:30 #24217

    • ic2
    • ic2's Avatar
    • Topic Author


  • Posts: 1574
  • Hello Fabrice,

    So I have to catch each one, and rewrite it the X# way.
    But any sample to have a better translation is welcome ! :)

    Wow, that's an awful lot of work indeed.

    I use it regularly and often it needs a bit of tweaking. Sometimes I don't see why it fails (like with these shortcut keys which obviously was me overlooking WPF). I will keep in mind to report if I needed to change something if this helps you!

    Dick

    Please Log in or Create an account to join the conversation.

    • Page:
    • 1