1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Visual Studio | WPF > マウス左クリック + Shift (またはAltキー)を検知 > Keyboard.IsKeyDown | Key.LeftShift | Key.LeftAlt

Last updated at Posted at 2017-11-10
動作環境
Windows 8.1 Pro (64bit)
Microsoft Visual Studio 2017 Community
Sublime Text 2

処理概要

Buttonの押下時にShiftキーまたはAltキーを検知する。

参考

プロジェクト設定

Visual Studio / WPF > WindowsアプリケーションにてSystem.Console.WriteLine()をコンソールに出力する

code

MainWindow.xaml
<Window x:Class="_171110_t1439_shiftKey.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_171110_t1439_shiftKey"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Width="120" Height="30" Content="Return fire!" Click="Button_Click"/>
    </Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace _171110_t1439_shiftKey
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (Keyboard.IsKeyDown(Key.LeftShift))
            {
                System.Console.WriteLine("PHOTON TOPEDO!");
            }
            else if (Keyboard.IsKeyDown(Key.LeftAlt))
            {
                System.Console.WriteLine("Alert: Self-destruct sequence is activated.");
            }
            else {
                System.Console.WriteLine("photon topede");
            }
        }
    }
}

結果

qiita.png

上から順番に、

  • 左Shiftを押しながら
  • 左Altを押しながら
  • 何も押さず

qiita.png

技術英語

  • return fire!: 「打ち返せ」
  • PHOTON TOPEDE: (光子)魚雷
  • Self-destruct sequence: 自爆シーケンス

検索用キーワード

(C++ Builderで使うもの)

  • ssAlt
  • ssShift
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?