繁体   English   中英

处理非输入击键?

[英]Handling non-input keystrokes?

我不确定如何说出我想要问的内容; 在C ++中,使用stdio.h头而不是iostream,如何在任何时候按下转义键,程序终止? 有什么我可以在程序的顶部添加一次,或者我是否必须将它添加到每个循环/条件单独? 下面是我的代码(sleep()函数仅用于可视加载/计算效果):

#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;
}

有什么我可以在程序的顶部添加一次,或者我是否必须将它添加到每个循环/条件单独?

我认为你可以添加一次,并在整个程序中使用它来轻松捕捉关键的中风事件。

以下是一个代码片段,显示了我用于处理控制台应用程序中的击键事件的函数。 它使用GetAsyncKeyState() 其中包含一个部分,显示如何捕获CTRL键,以及一旦看到它就可以执行某些操作 (显示的片段显示了如何捕获<ctrl><shift><h>键序列以显示使用此特定例程的帮助菜单。

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

代码段:

/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
//
//  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()

有许多方法可以在其他内容发生的同时调用GetAsyncKeyState() 线程是一个好方法。 您也可以使用while()/switch(){}组合在线完成所有操作。 这是一个非常简单的例子,说明如何做到这一点(伪代码)

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()

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

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

stdio中使用的终端很可能是线路缓冲(煮熟)。 如果是,则通过scanf检查转义键将不起作用。

查看这些网址:

从标准输入中捕获字符,无需等待按下输入

scanf没有读取输入

CURSES或NCURSES将检测转义键(ASCII字符27),具体取决于终端类型。

此代码可以在WINDOWS中用于检查ESCAPE密钥。

    #include <conio.h> 
    #include <ctype.h>

    int ch;

    _cputs( "Type 'Y' when finished typing keys: " );
    do
    {
      ch = _getch();
      ch = toupper( ch );

          if (ch != 27) {
           _cputs( "CHARACTER: " );
           _putch( ch );
           _putch( '\r' );    // Carriage return
           _putch( '\n' );    // Line feed
      }

    } while( ch != 27 );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM