处理非输入键击? [英] Handling non-input keystrokes?

查看:56
本文介绍了处理非输入键击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定该如何表达我想问的内容;在C ++中,使用stdio.h标头而不是iostream,我如何做到这一点,以便在任何时候按下转义键都会终止程序?是否可以在程序顶部添加一次,还是必须将其分别添加到每个循环/条件循环中?下面是我的代码(sleep()函数仅用于视觉加载/计算效果):

I'm not sure exactly how to phrase what I'm trying to ask; in C++, using the stdio.h header instead of iostream, how would I make it so that if the escape key is pressed at any point, the program is terminated? Is there something I could add once at the top of the program, or would I have to add it to every loop/conditional individually? Below is my code (the sleep() function is just for a visual loading/calculating effect):

#include <stdio.h>
#include <math.h>
#include <windows.h>

void repeat();
void quadratic()
{
    double a, b, c;
    double ans[2];
    printf("-Arrange your equation in the form aX^2+bX+c \n-Enter the value of a: ");
    scanf("%lf", &a);
    printf("-Enter the value of b: ");
    scanf("%lf", &b);
    printf("-Enter the value of c: ");
    scanf("%lf", &c);
    double radical=((b*b)-(4*a*c));
    double root=sqrt(radical);
    double negB=(-1)*b;
    double denominator=2*a;
    if(denominator==0)
    {
        printf("Calculating");
        Sleep(100);
        printf(".");
        Sleep(100);
        printf(".");
        Sleep(100);
        printf(".");
        Sleep(100);
        printf("\nError: Denominator must be non-zero.\n \n \n");

    }
    else if(radical==0)
    {
        ans[0]=negB/denominator;
        printf("Both roots are equal: both values are X=%lf\n \n \n", ans[0]);

    }
    else if(radical<0)
    {
        printf("Calculating");
        Sleep(100);
        printf(".");
        Sleep(100);
        printf(".");
        Sleep(100);
        printf(".");
        Sleep(100);
        double r,i;
        radical*=-1;
        r=negB/(2*a);
        i=sqrt(radical)/(2*a);
        printf("\nBoth roots are imaginary numbers.\n");
        printf("Non-real answer(s): X=%lf+%lfi X=%lf-%lfi\n \n \n",r,i,r,i);

    }
    else
    {
    ans[0]=(negB+root)/denominator;
    ans[1]=(negB-root)/denominator;
    printf("Calculating");
        Sleep(100);
        printf(".");
        Sleep(100);
        printf(".");
        Sleep(100);
        printf(".");
        Sleep(100);
        printf("\nX=%lf, X=%lf\n \n", ans[0], ans[1]);
    }
    repeat();

}
void repeat()
{
    quadratic();
}
int main(void)
{   
    quadratic();
    return 0;
}

推荐答案

我是否可以在程序顶部添加一次内容,还是必须将其分别添加到每个循环/条件中?em>

Is there something I could add once at the top of the program, or would I have to add it to every loop/conditional individually?

我认为您可以添加一次,然后在整个程序中使用它轻松捕获按键事件.

I think you can add something once, and use it to catch key stroke events easily throughout your program.

以下是一个代码段,显示了我用来在控​​制台应用程序中处理按键事件的功能.它使用 GetAsyncKeyState().包含的部分显示了如何捕获CTRL键,以及一旦看到后如何做某事.(所示的代码段显示了如何捕获< ctrl>< shift>< h> 键序列,以显示使用此特定例程的帮助菜单.

Following is a code snippet showing a function I have used to handle key stroke events in a console application. It uses GetAsyncKeyState(). Included is a section that shows how to capture a CTRL key, and how you can do something once you see it. (the snippet shown shows how I capture a <ctrl><shift><h> key sequence to display a help menu for using this particular routine.

注意: 在描述中, delay_x(浮动延迟)只是一个自定义的,非阻塞的睡眠或延迟功能,其中包括调用以下代码段.从主程序循环 while(1){...} 中调用它.按键组合之一提供了退出程序的方法:< CTRL>< SHIFT>< K>

Note: In the description, delay_x(float delay) is simply a custom, non-blocking sleep, or delay function that includes a call to the following snippet. It is called from within the main program loop while(1){...} . Exiting the program is provided in one of the keystroke combinations: <CTRL><SHIFT><K>

代码段:

Code snippet:

/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
//
//  SetAppState() is called continuously from within delay_x()
//  to capture keystroke combinations as they occur asynchronously
//  with this application, Keystroke combinations are listed below
//
//  Note: GetAsyncKeyState() can maintian information regarding the 
//        state of a key instantaineously by use the MSB, 
//        and recently by using the LSB.  
//        
//        For this application
//        only instantaineous information will be kept, minimizing
//        conflicts with other keyboard shortcut definitions 
//        defined by other applications that may be running
//        simultaineously.
//  
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////

void SetAppState(void)
{
    short state=0;
    short state1=0;

    state = GetAsyncKeyState(VK_CONTROL);
    if (0x80000000 & state) //check instantaineous state of key
    {
        state = GetAsyncKeyState(VK_SHIFT); 
        if (0x80000000 & state) //check instantaineous state of key
        {
            state = GetAsyncKeyState('h'); 
            state1 = GetAsyncKeyState('H'); 
            if ((0x80000000 & state) || 
                (0x80000000 & state1))
            {    sprintf(gTempBuf, "Usage - keystrokes to access and control the PaAutoStartSlot application:\n\n"
                                   "<CTRL><SHIFT> H   (H)elp -    \n"
                                   "<CTRL><SHIFT> V  o(V)erride - \n"
                                   "<CTRL><SHIFT> S   (S)tatus -  \n"
                                   "<CTRL><SHIFT> K   (K)ill -    \n"
                                   "<CTRL><SHIFT> N   (N)o -      \n"
                                   "<CTRL><SHIFT> I   (I)Inside - \n"
                                   "<CTRL><SHIFT> O   (O)Outside- \n"
                                   "\nSee log file at this location for runtime errors: \n\n%s", LOGFILE); 
                 MessagePopup("Usage Menu",gTempBuf);
            }

///// ... more code ...
End of snippet  

编辑 -回答注释中的问题如何调用GetAsyncKeyState()

有很多方法可以同时调用 other GetAsyncKeyState().线程是一个好方法.您也可以使用 while()/switch(){} 组合直接完成所有操作.这是一个非常简单的示例(使用伪代码)

There are many ways you could call GetAsyncKeyState() at the same time other stuff is going on. Threads are a good way. You can also do it all in line using a while()/switch(){} combination. Here is a very simple example of how you could do this (in pseudo code)

int gRunning = 1;
int state = 1; 

int main(void)
{

    //create variables, initialize stuff
    while(gRunning)//this is your main program loop
    {
        delay_x(1.0);//check for keystrokes
        switch(state)  {
            case 1:
                //do some stuff here  
                //and experiment with values passed to delay_x(n)
                delay_x(10000);//check for keystrokes
                state++;
                break;
            case 2:
                //do some different stuff here
                delay_x(10000);//check for keystrokes
                state++;
                break;
            ... Add as many cases as you need for your program.
            case n://last case, set execution flow to top
                //do some more different stuff here
                delay_x(10000);//check for keystrokes
                state = 1;//loop back to top
                break;
         }
     }
    return 0;
}
void delay_x (float delay)
{   
    static clock_t time1;   
    static clock_t time2;   clock();    

    time1 = clock();    
    time2 = clock();    

    while ((time2 - time1) < delay) 
    {       
        time2 = clock();        
        SetAppState(); //see partial definition in my original answer above.
    }       
}

注意: :使用此方法,您可以根据需要设置任意数量的 cases ,重要的是保持对 GetAsyncKeyState()的稳定调用.通过调用 delay_x().

Note: Using this method, you can have as many, or as few, cases as you need, the important thing is to keep a steady flow of calls to GetAsyncKeyState(). This does that via the call to delay_x().

注2: :以下是导致程序退出的段(添加到上面的定义中):

Note2: Here is the segment (added to above definition) that will cause your program to exit:

    state = GetAsyncKeyState('k'); 
    state1 = GetAsyncKeyState('K'); 
    if ((0x80000000 & state) || 
        (0x80000000 & state1))
    {    
        printf("Kill Program"); 
        gRunning = FALSE;
    }

这篇关于处理非输入键击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆