Мое решение не является идеальным и требует много дополнительных проверок.
В любом случае, это отправная точка для ваших нужд.
Прежде всего, установите KeyPreview = true
в вашей форме.
Затем используйте:
public partial class Form1 : Form
{
private bool reading = false;
private byte dirSize = 2;
private byte filesize = 2;
private string keys = "";
private const string defExt = ".png";
private string exePath =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.O)
{
reading = true;
keys = "";
e.Handled = true;
return;
}
if (reading)
{
Char ch = (char)(e.KeyValue);
if (Char.IsLetterOrDigit(ch))
{
keys += ch;
e.Handled = true;
}
if (keys.Length == (dirSize + filesize))
{
string dir = Path.Combine(exePath, keys.Substring(0, dirSize));
string filename = keys.Substring(dirSize, filesize) + defExt;
string fullPath = Path.Combine(dir, filename);
if (File.Exists(fullPath))
pictureBox1.Image = Image.FromFile(fullPath);
reading = false;
}
}
}
}