c# - C# 在按键时切换布尔值
问题描述
我目前有一个名为 debug 的 bool。我想要它,这样当我按下 F10 时,它会将布尔值设置为 true,然后如果我再次按下它,则返回 false,依此类推。
这是我正在使用的代码:
bool debug = false;
if (cVersion < oVersion)
{
Process.Start("http://consol.cf/update.php");
return;
}
for (; ; )
{
if (debug)
{
Console.WriteLine("Please type in a command");
cmd = Console.ReadLine();
p.Send(cmd);
}
else
{
Console.WriteLine("Press enter to execute config");
Console.ReadLine();
WebConfigReader conf =
new WebConfigReader(url);
string[] tokens = Regex.Split(conf.ReadString(), @"\r?\n|\r");
foreach (string s in tokens)
//ConsoleConfig cons = new ConsoleConfig();
{
p.Send(s);
//p.Send(test);
}
}
提前致谢。
解决方案
bool debug = false;
public void Toggle()
{
ConsoleKeyInfo keyinfo = Console.ReadKey();
if (keyinfo.Key == ConsoleKey.F10)
{
debug = !debug;
if(debug)
{
//Your code here if debug = true
}
else
{
//Your code here if debug = false
}
}
else
{
//Your code here if key press other than F10
}
}
ConsoleKeyInfo:描述被按下的控制台键,包括控制台键所代表的字符和SHIFT、ALT、CTRL修饰键的状态。
试一次可能对你有帮助。
推荐阅读
- olap - 分布式引擎上的 TopK 函数在 clickhouse 中仅返回 10 条记录
- php - Docker + Laravel 问题 - “ViewException”
- swagger - 使用 swagger-ui-express 和 swagger-jsdoc 记录时,如何指向 json 文件进行架构定义?
- android - 在空引用 kotlin 扩展上调用方法
- python - 我正在编写一个程序,它为从 (1, 1) 到 (5, 5) 的第一象限中的所有点生成整数坐标列表
- android - 如何使用颤振 dio 下载文件?
- amazon-web-services - 是否有工具可以使用 CloudWatch putMetric API 将 AWS QLDB 表中的全部内容发布到 Cloudwatch?
- mqtt - 用户:Bob 在地址 test/signal/abc 上没有队列 bob.test/test/signal/abc 的权限 ='CREATE_DURABLE_QUEUE'
- php - 错误号:1064 CodeIgniter 查询生成器
- java - Azure BlockBlobClient downloadWithResponse 文件大小问题