Skip to content

Instantly share code, notes, and snippets.

@masaru-b-cl
Created April 26, 2012 08:41
Show Gist options
  • Save masaru-b-cl/2497694 to your computer and use it in GitHub Desktop.
Save masaru-b-cl/2497694 to your computer and use it in GitHub Desktop.
こうですか?わかりません><
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
class FunctionKeyPressComponent : Component
{
private Form target;
public Form Target
{
get { return target; }
set {
target = value;
if (target == null) return;
target.KeyDown += (s, e) =>
{
if (Keys.F1 <= e.KeyCode && e.KeyCode <= Keys.F12)
{
target.Text = e.KeyCode.ToString() + " Pressed!";
// ファンクションキーの基底の動作を握り潰す
e.Handled = true;
}
};
}
}
}
}
@kazuk
Copy link

kazuk commented Apr 26, 2012

こうだと思います。

public partial class OmmitFunctionKeyDefaultActions : Component
{
    public OmmitFunctionKeyDefaultActions()
    {
        InitializeComponent();
        Disposed += OnDisposed;
    }

    public OmmitFunctionKeyDefaultActions(IContainer container)
    {
        container.Add(this);
        InitializeComponent();
        Disposed += OnDisposed;
    }

    private Form _targetForm;
    public Form TargetForm
    {
        get { return _targetForm; }
        set
        {
            if( _targetForm==value ) return;
            if (_targetForm != null)
            {
                _targetForm.KeyDown -= OnKeyDown;
            }
            _targetForm = value;
            if (_targetForm != null)
            {
                _targetForm.KeyDown += OnKeyDown;
            }
        }
    }

    private void OnDisposed(object sender, EventArgs e)
    {
        TargetForm = null;
    }

    private void OnKeyDown(object sender, KeyEventArgs e)
    {
        if (Keys.F1 <= e.KeyCode && e.KeyCode <= Keys.F12)
        {
            // ファンクションキーの基底の動作を握り潰す
            e.Handled = true;            
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment