Re: Using msvcrt (in Windows), how to catch Enter key?
En Tue, 30 Oct 2007 01:46:19 -0300, Dick Moores <[EMAIL PROTECTED]> escribió: > At 06:34 PM 10/29/2007, Gabriel Genellina wrote: >> The underlying function in Windows is Sleep (or SleepEx) which takes an >> argument in milliseconds. 0.0001s = 0.1ms and it's rounded to 0. > > Ah, useful information. Thank you. Where'd you learn that? Microsoft's Windows API Reference: http://msdn2.microsoft.com/en-us/library/aa383749.aspx The Sleep function is at http://msdn2.microsoft.com/en-us/library/ms686298.aspx -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Using msvcrt (in Windows), how to catch Enter key?
At 06:34 PM 10/29/2007, Gabriel Genellina wrote: >En Mon, 29 Oct 2007 21:22:36 -0300, Dick Moores <[EMAIL PROTECTED]> escribió: > > > At 03:23 PM 10/29/2007, Gabriel Genellina wrote: > >> En Mon, 29 Oct 2007 14:39:49 -0300, Dick Moores <[EMAIL PROTECTED]> > >> escribió: > >> > >> > >> At least add a small sleep() call inside the loop, to be nice to other > >> running processes: > >> > > > > Yes, that makes a major difference in the CPU > > usage percentage on my computer. In fact I can't > > even tell that there is anything going on other > > than the usual behind-the-scenes XP stuff. CPU > > usage stays right around 0% or 6%, with an > > occasional 6% and a very occasional 15%. > > Interestingly, sleep(0.001) makes as big a > > difference as your sleep(0.1), but sleep(0.0001) bumps it up to a steady > > 100%! > >The underlying function in Windows is Sleep (or SleepEx) which takes an >argument in milliseconds. 0.0001s = 0.1ms and it's rounded to 0. Sleep(0) >has very specific semantics - for a single threaded program, it does >nothing, so your code is effectively a busy loop taking 100% CPU. Ah, useful information. Thank you. Where'd you learn that? Dick Moores -- http://mail.python.org/mailman/listinfo/python-list
Re: [Possible SPAM] Re: Using msvcrt (in Windows), how to catch Enter key?
En Mon, 29 Oct 2007 21:22:36 -0300, Dick Moores <[EMAIL PROTECTED]> escribió: > At 03:23 PM 10/29/2007, Gabriel Genellina wrote: >> En Mon, 29 Oct 2007 14:39:49 -0300, Dick Moores <[EMAIL PROTECTED]> >> escribió: >> >> >> At least add a small sleep() call inside the loop, to be nice to other >> running processes: >> > > Yes, that makes a major difference in the CPU > usage percentage on my computer. In fact I can't > even tell that there is anything going on other > than the usual behind-the-scenes XP stuff. CPU > usage stays right around 0% or 6%, with an > occasional 6% and a very occasional 15%. > Interestingly, sleep(0.001) makes as big a > difference as your sleep(0.1), but sleep(0.0001) bumps it up to a steady > 100%! The underlying function in Windows is Sleep (or SleepEx) which takes an argument in milliseconds. 0.0001s = 0.1ms and it's rounded to 0. Sleep(0) has very specific semantics - for a single threaded program, it does nothing, so your code is effectively a busy loop taking 100% CPU. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Using msvcrt (in Windows), how to catch Enter key?
Reposting, deleting the [Possible SPAM] from the Subject: header. At 03:23 PM 10/29/2007, Gabriel Genellina wrote: >En Mon, 29 Oct 2007 14:39:49 -0300, Dick Moores <[EMAIL PROTECTED]> escribió: > > > But here's a case where it seems I do need the > > > > if msvcrt.kbhit() line > >At least add a small sleep() call inside the loop, to be nice to other >running processes: > > > = > > #!/usr/bin/env python > > #coding=utf-8 > > import time > > import msvcrt > > timeNow = time.time() > > oldTimeNow = timeNow > > while True: > > if msvcrt.kbhit(): > > key = msvcrt.getch() > > if key == 'h': > > print 'Hello' > > if key == 'b': > > print 'Bye' > > if key == '\r': # Enter key > > break > else: > time.sleep(0.1) > > timeNow = time.time() > > if timeNow - oldTimeNow > 5: > > print "5 seconds passed" > > oldTimeNow = timeNow > > = Yes, that makes a major difference in the CPU usage percentage on my computer. In fact I can't even tell that there is anything going on other than the usual behind-the-scenes XP stuff. CPU usage stays right around 0% or 6%, with an occasional 6% and a very occasional 15%. Interestingly, sleep(0.001) makes as big a difference as your sleep(0.1), but sleep(0.0001) bumps it up to a steady 100%! Thanks, Dick -- http://mail.python.org/mailman/listinfo/python-list
Re: [Possible SPAM] Re: Using msvcrt (in Windows), how to catch Enter key?
At 03:23 PM 10/29/2007, Gabriel Genellina wrote: >En Mon, 29 Oct 2007 14:39:49 -0300, Dick Moores <[EMAIL PROTECTED]> escribió: > > > But here's a case where it seems I do need the > > > > if msvcrt.kbhit() line > >At least add a small sleep() call inside the loop, to be nice to other >running processes: > > > = > > #!/usr/bin/env python > > #coding=utf-8 > > import time > > import msvcrt > > timeNow = time.time() > > oldTimeNow = timeNow > > while True: > > if msvcrt.kbhit(): > > key = msvcrt.getch() > > if key == 'h': > > print 'Hello' > > if key == 'b': > > print 'Bye' > > if key == '\r': # Enter key > > break > else: > time.sleep(0.1) > > timeNow = time.time() > > if timeNow - oldTimeNow > 5: > > print "5 seconds passed" > > oldTimeNow = timeNow > > = Yes, that makes a major difference in the CPU usage percentage on my computer. In fact I can't even tell that there is anything going on other than the usual behind-the-scenes XP stuff. CPU usage stays right around 0% or 6%, with an occasional 6% and a very occasional 15%. Interestingly, sleep(0.001) makes as big a difference as your sleep(0.1), but sleep(0.0001) bumps it up to a steady 100%! Thanks, Dick -- http://mail.python.org/mailman/listinfo/python-list
Re: Using msvcrt (in Windows), how to catch Enter key?
Dick Moores wrote: > Windows XP Pro, Python 2.5.1 > > import msvcrt > while True: > if msvcrt.kbhit(): > key = msvcrt.getch() > if key == 'Enter' >do something > > Is there a way to catch the pressing of the 'Enter' key? > > Thanks, > > Dick Moores > You have examples for this in http://effbot.org/librarybook/msvcrt.htm HTH -- http://mail.python.org/mailman/listinfo/python-list
Re: Using msvcrt (in Windows), how to catch Enter key?
En Mon, 29 Oct 2007 14:39:49 -0300, Dick Moores <[EMAIL PROTECTED]> escribió: > But here's a case where it seems I do need the > > if msvcrt.kbhit() line At least add a small sleep() call inside the loop, to be nice to other running processes: > = > #!/usr/bin/env python > #coding=utf-8 > import time > import msvcrt > timeNow = time.time() > oldTimeNow = timeNow > while True: > if msvcrt.kbhit(): > key = msvcrt.getch() > if key == 'h': > print 'Hello' > if key == 'b': > print 'Bye' > if key == '\r': # Enter key > break else: time.sleep(0.1) > timeNow = time.time() > if timeNow - oldTimeNow > 5: > print "5 seconds passed" > oldTimeNow = timeNow > == -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Using msvcrt (in Windows), how to catch Enter key?
At 09:53 AM 10/29/2007, Dick Moores wrote: >At 09:26 AM 10/29/2007, Gabriel Genellina wrote: > >On 29 oct, 09:23, Dick Moores <[EMAIL PROTECTED]> wrote: > > > > > >while True: > > > > if msvcrt.getch() == '\r': > > > > > > I tried it and find that without the msvcrt.kbhit the first key I hit > > > doesn't do anything. I have to hit that key again, or another key. > > > >I'd say there is a logic error in your program then; keys don't "do > >anything" by themselves. > >Try posting a small sample, telling what you get and what you expect. > >Huh. Works now. > >import msvcrt >while True: > key = msvcrt.getch() > if key == 'h': > print 'Hello' > if key == 'b': > print 'Bye' > if key == '\r': # 'Enter' key > break > >Dick But here's a case where it seems I do need the if msvcrt.kbhit() line = #!/usr/bin/env python #coding=utf-8 import time import msvcrt timeNow = time.time() oldTimeNow = timeNow while True: if msvcrt.kbhit(): key = msvcrt.getch() if key == 'h': print 'Hello' if key == 'b': print 'Bye' if key == '\r': # Enter key break timeNow = time.time() if timeNow - oldTimeNow > 5: print "5 seconds passed" oldTimeNow = timeNow == Without that line: == #!/usr/bin/env python #coding=utf-8 import time import msvcrt timeNow = time.time() oldTimeNow = timeNow while True: #if msvcrt.kbhit(): key = msvcrt.getch() if key == 'h': print 'Hello' if key == 'b': print 'Bye' if key == '\r': # Enter key break timeNow = time.time() if timeNow - oldTimeNow > 5: print "5 seconds passed" oldTimeNow = timeNow Without that line the "5 seconds passed" report is printed ONLY after a "b" or an "h", not what I want. Dick -- http://mail.python.org/mailman/listinfo/python-list
Re: Using msvcrt (in Windows), how to catch Enter key?
At 09:26 AM 10/29/2007, Gabriel Genellina wrote: >On 29 oct, 09:23, Dick Moores <[EMAIL PROTECTED]> wrote: > > > >while True: > > > if msvcrt.getch() == '\r': > > > > I tried it and find that without the msvcrt.kbhit the first key I hit > > doesn't do anything. I have to hit that key again, or another key. > >I'd say there is a logic error in your program then; keys don't "do >anything" by themselves. >Try posting a small sample, telling what you get and what you expect. Huh. Works now. import msvcrt while True: key = msvcrt.getch() if key == 'h': print 'Hello' if key == 'b': print 'Bye' if key == '\r': # 'Enter' key break Dick -- http://mail.python.org/mailman/listinfo/python-list
Re: Using msvcrt (in Windows), how to catch Enter key?
On 29 oct, 09:23, Dick Moores <[EMAIL PROTECTED]> wrote: > >while True: > > if msvcrt.getch() == '\r': > > I tried it and find that without the msvcrt.kbhit the first key I hit > doesn't do anything. I have to hit that key again, or another key. I'd say there is a logic error in your program then; keys don't "do anything" by themselves. Try posting a small sample, telling what you get and what you expect. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Using msvcrt (in Windows), how to catch Enter key?
At 04:29 AM 10/29/2007, Filip Wasilewski wrote: >On Oct 29, 11:26 am, Dick Moores <[EMAIL PROTECTED]> wrote: > > Windows XP Pro, Python 2.5.1 > > > > import msvcrt > > while True: > > if msvcrt.kbhit(): > > key = msvcrt.getch() > > if key == 'Enter' > > do something > > > > Is there a way to catch the pressing of the 'Enter' key? > >Yes there is. Just open the Python shell and see what is being >returned by `getch` or `getche` functions when you press Enter: > > >>> import msvcrt > >>> msvcrt.getch() >'\r' Terrific! Thanks. >Also try to avoid `busy waiting` and calling msvcrt.kbhit in a loop >without a sleep statement. >I don't know your case but probably this >should be enough: > >while True: > if msvcrt.getch() == '\r': I tried it and find that without the msvcrt.kbhit the first key I hit doesn't do anything. I have to hit that key again, or another key. Dick -- http://mail.python.org/mailman/listinfo/python-list
Re: Using msvcrt (in Windows), how to catch Enter key?
On Oct 29, 4:26 am, Dick Moores <[EMAIL PROTECTED]> wrote: > Windows XP Pro, Python 2.5.1 > > import msvcrt > while True: > if msvcrt.kbhit(): > key = msvcrt.getch() > if key == 'Enter' > do something > > Is there a way to catch the pressing of the 'Enter' key? > > Thanks, > > Dick Moores Let's find out: >>> from msvcrt import getch >>> while True: ... key = getch() ... if key: print repr(key)+',', ... 'p', 'r', 'e', 's', 's', 'i', 'n', 'g', ' ', 'e', 'n', 't', 'e', 'r', ':', ' ', '\r', Gee, I pressed enter, and it returned '\r'. I wonder... >>> import msvcrt >>> while True: ... if msvcrt.kbhit(): ... key = msvcrt.getch() ... if key == '\r': ... print "success!" ... success! -- http://mail.python.org/mailman/listinfo/python-list
Re: Using msvcrt (in Windows), how to catch Enter key?
On Oct 29, 11:26 am, Dick Moores <[EMAIL PROTECTED]> wrote: > Windows XP Pro, Python 2.5.1 > > import msvcrt > while True: > if msvcrt.kbhit(): > key = msvcrt.getch() > if key == 'Enter' > do something > > Is there a way to catch the pressing of the 'Enter' key? Yes there is. Just open the Python shell and see what is being returned by `getch` or `getche` functions when you press Enter: >>> import msvcrt >>> msvcrt.getch() '\r' Also try to avoid `busy waiting` and calling msvcrt.kbhit in a loop without a sleep statement. I don't know your case but probably this should be enough: while True: if msvcrt.getch() == '\r': ... fw -- http://mail.python.org/mailman/listinfo/python-list
Using msvcrt (in Windows), how to catch Enter key?
Windows XP Pro, Python 2.5.1 import msvcrt while True: if msvcrt.kbhit(): key = msvcrt.getch() if key == 'Enter' do something Is there a way to catch the pressing of the 'Enter' key? Thanks, Dick Moores -- http://mail.python.org/mailman/listinfo/python-list