nanoFramework.Iot.Device.KeyMatrix 1.2.907

Prefix Reserved
dotnet add package nanoFramework.Iot.Device.KeyMatrix --version 1.2.907
                    
NuGet\Install-Package nanoFramework.Iot.Device.KeyMatrix -Version 1.2.907
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="nanoFramework.Iot.Device.KeyMatrix" Version="1.2.907" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="nanoFramework.Iot.Device.KeyMatrix" Version="1.2.907" />
                    
Directory.Packages.props
<PackageReference Include="nanoFramework.Iot.Device.KeyMatrix" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add nanoFramework.Iot.Device.KeyMatrix --version 1.2.907
                    
#r "nuget: nanoFramework.Iot.Device.KeyMatrix, 1.2.907"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package nanoFramework.Iot.Device.KeyMatrix@1.2.907
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=nanoFramework.Iot.Device.KeyMatrix&version=1.2.907
                    
Install as a Cake Addin
#tool nuget:?package=nanoFramework.Iot.Device.KeyMatrix&version=1.2.907
                    
Install as a Cake Tool

Key Matrix

An M×N key matrix driver.

(M is number of output pins and N is number of input pins.)

Documentation

These key matrices look like this:

4x4-Keypad

This is a 4×4 matrix. And here is the schematic

You can connect any M×N key matrix, theoretically, by using M+N GPIO pins.

You can also use any compatible GPIO controller like Mcp23xxx instead of native controller.

Usage

You need to create 2 lists of int, one for the input and one for the output.

int[] outputs = new int[] { 26, 19, 13, 6 };
int[] inputs = new int[] { 21, 20, 16, 12 };
KeyMatrix mk = new KeyMatrix(outputs, inputs, TimeSpan.FromMilliseconds(20));

You can use as well any GpioController like the MCP23017 in the following example

var settings = new System.Device.I2c.I2cConnectionSettings(1, 0x20);
var i2cDevice = System.Device.I2c.I2cDevice.Create(settings);
var mcp23017 = new Iot.Device.Mcp23xxx.Mcp23017(i2cDevice);
GpioController gpio = new GpioController(PinNumberingScheme.Logical, mcp23017);
int[] outputs = new int[] { 26, 19, 13, 6 };
int[] inputs = new int[] { 21, 20, 16, 12 };
KeyMatrix mk = new KeyMatrix(outputs, inputs, TimeSpan.FromMilliseconds(20), gpio, true);

Read on Key

To read a key, just the ReadKey function:

KeyMatrixEvent? key = mk.ReadKey();

KeyMatrixEvent contains the event that happened. Please note that ReadKey is blocked up to the moment an event is detected.

Event based approach

KeyMatrix supports events. Just subscribe to the event and have a function to handle the events:

Debug.WriteLine("This will now start listening to events and display them for 60 seconds.");
mk.KeyEvent += KeyMatrixEventReceived;
mk.StartListeningKeyEvent();
Thread.Sleep(60000);

mk.StopListeningKeyEvent();

void KeyMatrixEventReceived(object sender, KeyMatrixEvent keyMatrixEvent)
{
    // Do something here, you have an event!
}

Tips and tricks

  • Using diodes(eg. 1N4148) for each button prevents "ghosting" or "masking" problem.
  • Input pins need pull-down resistors connect to ground if your MCU doesn't have it. So you need to have a pull-down on a the MCU for example.
  • If your key matrix doesn't work well, try to swap output and input pins. Some includes diodes and if they are used the reverse way won't work properly.

Key Matrix Samples

This shows how to connect the matrix.

Important: Please make you don't forget to place a pull down on the input matrix.

Connection using on-board GPIO

Connection using MCU

Connection using MCP23017

Connection using a MCP23017

Product Compatible and additional computed target framework versions.
.NET Framework net is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.2.907 171 10/2/2025
1.2.864 255 4/2/2025
1.2.852 277 3/11/2025
1.2.822 203 2/26/2025
1.2.772 202 2/4/2025
1.2.755 209 1/31/2025
1.2.737 182 1/13/2025
1.2.696 198 12/16/2024
1.2.673 196 10/23/2024
1.2.631 186 8/28/2024
1.2.570 215 6/14/2024
1.2.560 209 5/29/2024
1.2.548 180 5/15/2024
1.2.436 295 11/10/2023
1.2.329 273 5/26/2023
1.2.313 236 5/12/2023
1.2.297 259 5/3/2023
1.2.203 429 12/28/2022
1.2.141 501 10/25/2022
1.2.122 555 10/12/2022
1.2.114 481 10/8/2022
1.2.95 498 9/22/2022
1.2.87 572 9/15/2022
1.2.73 523 9/8/2022
1.1.113.2032 521 6/23/2022
1.1.58.10097 562 5/23/2022
1.1.1 580 4/14/2022
1.0.300 525 3/31/2022
1.0.277-preview.125 223 3/25/2022
1.0.277-preview.110 225 3/18/2022
1.0.277-preview.105 228 3/15/2022
1.0.277-preview.98 231 3/8/2022
1.0.277-preview.77 225 2/18/2022
1.0.277-preview.41 239 1/28/2022
1.0.277-preview.32 243 1/27/2022
1.0.277-preview.17 247 1/24/2022
1.0.277-preview.15 230 1/21/2022
1.0.277-preview.13 239 1/21/2022
1.0.277-preview.1 236 1/11/2022
1.0.259 424 12/9/2021
1.0.218 313 10/18/2021
1.0.155 434 8/31/2021
1.0.135 416 7/16/2021
1.0.134 287 7/15/2021
1.0.133 303 7/14/2021
1.0.129 273 7/6/2021
1.0.125 305 7/5/2021
1.0.121 315 6/29/2021
1.0.119 338 6/28/2021
1.0.33 282 5/24/2021